Skip to content

Commit

Permalink
Fix for Bug#114846 (Bug#36574322), Auto-closeable X dev session.
Browse files Browse the repository at this point in the history
Change-Id: Iae79e3ddf89daaa68a58d58bfe2a08c801ab377a
  • Loading branch information
fjssilva committed May 15, 2024
1 parent 223dfaf commit a9ec867
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

Version 9.0.0

- Fix for Bug#114846 (Bug#36574322), Auto-closeable X dev session.
Thanks to Daniel Kec for his contribution.

- Fix for Bug#114989 (Bug#36612566), Setting null value in setClientInfo throws an NPE.

- WL#16376, Set 'caching_sha2_password' as default fallback authentication plugin.
Expand Down
3 changes: 2 additions & 1 deletion src/main/user-api/java/com/mysql/cj/xdevapi/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* <p>
* The Client object is obtained via {@link ClientFactory#getClient(String, java.util.Properties)} or {@link ClientFactory#getClient(String, String)} methods.
*/
public interface Client {
public interface Client extends AutoCloseable {

/**
* Get <code>Session</code> from pool or the new one.
Expand All @@ -41,6 +41,7 @@ public interface Client {
* Calling the method <code>close</code> on a <code>Client</code>
* object that is already closed is a no-op.
*/
@Override
public void close();

public enum ClientProperty {
Expand Down
3 changes: 2 additions & 1 deletion src/main/user-api/java/com/mysql/cj/xdevapi/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
* <p>
* Users of the CRUD API do not need to escape identifiers. This is true for working with collections and for working with relational tables.
*/
public interface Session {
public interface Session extends AutoCloseable {

/**
* Retrieve the list of Schema objects for which the current user has access.
Expand Down Expand Up @@ -143,6 +143,7 @@ public interface Session {
/**
* Close this session.
*/
@Override
void close();

/**
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/testsuite/x/devapi/SessionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2533,4 +2533,41 @@ public void testBug97269() throws Exception {
}
}

@Test
void testAutoCloseableSession() throws Exception {
final Session testSessionRef;

try (Session testSession = this.fact.getSession(this.baseUrl)) {
assertTrue(testSession.isOpen());
testSessionRef = testSession;
}

assertNotNull(testSessionRef);
assertFalse(testSessionRef.isOpen());
}

@Test
void testAutoCloseableClient() throws Exception {
final ClientFactory testClientFactory = new ClientFactory();
final Client testClientRef;
final Session testSessionRef1;
final Session testSessionRef2;

try (Client testClient = testClientFactory.getClient(this.baseUrl, new Properties())) {
testSessionRef1 = testClient.getSession();
assertTrue(testSessionRef1.isOpen());
testSessionRef2 = testClient.getSession();
assertTrue(testSessionRef2.isOpen());
testClientRef = testClient;
}

assertNotNull(testSessionRef1);
assertFalse(testSessionRef1.isOpen());
assertNotNull(testSessionRef2);
assertFalse(testSessionRef2.isOpen());

assertNotNull(testClientRef);
assertThrows(XDevAPIError.class, "Client is closed\\.", testClientRef::getSession);
}

}

0 comments on commit a9ec867

Please sign in to comment.