Skip to content

Commit d094e58

Browse files
committed
Added autoclosing of YdbContext
1 parent dc75bfb commit d094e58

File tree

3 files changed

+35
-25
lines changed

3 files changed

+35
-25
lines changed

jdbc/src/main/java/tech/ydb/jdbc/YdbDriver.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,25 @@ public YdbConnection connect(String url, Properties info) throws SQLException {
5252
config.getSafeProps()
5353
});
5454

55-
if (config.isCacheConnectionsInDriver()) {
56-
return new YdbConnectionImpl(getCachedContext(config));
55+
if (!config.isCacheConnectionsInDriver()) {
56+
final YdbContext context = YdbContext.createContext(config);
57+
return new YdbConnectionImpl(context) {
58+
@Override
59+
public void close() throws SQLException {
60+
super.close();
61+
context.close();
62+
}
63+
};
5764
}
5865

59-
// findOrCreateJdbcParams new context
60-
final YdbContext context = YdbContext.createContext(config);
61-
return new YdbConnectionImpl(context) {
66+
YdbContext cached = getCachedContext(config);
67+
return new YdbConnectionImpl(cached) {
6268
@Override
6369
public void close() throws SQLException {
6470
super.close();
65-
context.close();
71+
if (!cached.hasConnections() && cache.remove(config, cached)) {
72+
cached.close();
73+
}
6674
}
6775
};
6876
}

jdbc/src/main/java/tech/ydb/jdbc/context/YdbContext.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ public void close() {
161161
}
162162
}
163163

164+
public boolean hasConnections() {
165+
return connectionsCount.get() > 0;
166+
}
167+
164168
public void register() {
165169
int actual = connectionsCount.incrementAndGet();
166170
int maxSize = tableClient.sessionPoolStats().getMaxSize();

jdbc/src/test/java/tech/ydb/jdbc/YdbDriverIntegrationTest.java

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,13 @@ public void connect() throws SQLException {
4343

4444
@Test
4545
public void testContextCache() throws SQLException {
46-
YdbContext ctx;
47-
try (Connection conn1 = DriverManager.getConnection(jdbcURL.build())) {
48-
Assertions.assertTrue(conn1.isValid(5000));
46+
Connection firstConnection = DriverManager.getConnection(jdbcURL.build());
47+
Assertions.assertTrue(firstConnection.isValid(5000));
4948

50-
YdbConnection unwrapped = conn1.unwrap(YdbConnection.class);
51-
Assertions.assertNotNull(unwrapped.getCtx());
49+
YdbConnection first = firstConnection.unwrap(YdbConnection.class);
50+
Assertions.assertNotNull(first.getCtx());
5251

53-
ctx = unwrapped.getCtx();
54-
}
52+
YdbContext ctx = first.getCtx();
5553

5654
try (Connection conn2 = DriverManager.getConnection(jdbcURL.build())) {
5755
Assertions.assertTrue(conn2.isValid(5000));
@@ -87,10 +85,7 @@ public void testContextCache() throws SQLException {
8785
Assertions.assertNotSame(ctx, unwrapped.getCtx());
8886
}
8987

90-
if (YdbDriver.isRegistered()) {
91-
YdbDriver.deregister();
92-
YdbDriver.register();
93-
}
88+
firstConnection.close();
9489

9590
try (Connection conn6 = DriverManager.getConnection(jdbcURL.build())) {
9691
Assertions.assertTrue(conn6.isValid(5000));
@@ -103,24 +98,27 @@ public void testContextCache() throws SQLException {
10398

10499
@Test
105100
public void testContextCacheConncurrent() throws SQLException {
106-
List<CompletableFuture<YdbContext>> list = new ArrayList<>();
101+
List<CompletableFuture<YdbConnection>> list = new ArrayList<>();
107102

108103
for (int idx = 0; idx < 20; idx++) {
109104
list.add(CompletableFuture.supplyAsync(() -> {
110-
try (Connection conn1 = DriverManager.getConnection(jdbcURL.build())) {
111-
YdbConnection unwrapped = conn1.unwrap(YdbConnection.class);
112-
Assertions.assertNotNull(unwrapped.getCtx());
113-
return unwrapped.getCtx();
105+
try {
106+
Connection connection = DriverManager.getConnection(jdbcURL.build());
107+
return connection.unwrap(YdbConnection.class);
114108
} catch (SQLException ex) {
115109
throw new RuntimeException("Cannot connect", ex);
116110
}
117111
}));
118112
}
119113

120-
YdbContext first = list.get(0).join();
114+
YdbContext first = list.get(0).join().getCtx();
115+
116+
for (CompletableFuture<YdbConnection> future: list) {
117+
Assertions.assertEquals(first, future.join().getCtx());
118+
}
121119

122-
for (CompletableFuture<YdbContext> future: list) {
123-
Assertions.assertEquals(first, future.join());
120+
for (CompletableFuture<YdbConnection> future: list) {
121+
future.join().close();
124122
}
125123
}
126124

0 commit comments

Comments
 (0)