11package tech .ydb .jdbc ;
22
33import java .sql .Connection ;
4+ import java .sql .Driver ;
45import java .sql .DriverManager ;
56import java .sql .SQLException ;
67import java .util .ArrayDeque ;
@@ -29,6 +30,25 @@ public class YdbDriverIntegrationTest {
2930
3031 private static final JdbcUrlHelper jdbcURL = new JdbcUrlHelper (ydb );
3132
33+ @ Test
34+ public void registerTest () throws SQLException {
35+ try {
36+ YdbDriver .deregister ();
37+ } catch (IllegalStateException ex ) {
38+ // skip error if driver was deregistered already
39+ }
40+
41+ IllegalStateException ex1 = Assertions .assertThrows (IllegalStateException .class , () -> YdbDriver .deregister ());
42+ Assertions .assertEquals (YdbConst .DRIVER_IS_NOT_REGISTERED , ex1 .getMessage ());
43+
44+ Assertions .assertFalse (YdbDriver .isRegistered ());
45+ YdbDriver .register ();
46+ Assertions .assertTrue (YdbDriver .isRegistered ());
47+
48+ IllegalStateException ex2 = Assertions .assertThrows (IllegalStateException .class , () -> YdbDriver .register ());
49+ Assertions .assertEquals (YdbConst .DRIVER_IS_ALREADY_REGISTERED , ex2 .getMessage ());
50+ }
51+
3252 @ Test
3353 public void connect () throws SQLException {
3454 try (Connection connection = DriverManager .getConnection (jdbcURL .build ())) {
@@ -43,8 +63,15 @@ public void connect() throws SQLException {
4363
4464 @ Test
4565 public void testContextCache () throws SQLException {
66+ Driver jdbcDriver = DriverManager .getDriver (jdbcURL .build ());
67+ Assertions .assertTrue (jdbcDriver instanceof YdbDriver );
68+
69+ YdbDriver driver = (YdbDriver )jdbcDriver ;
70+ Assertions .assertEquals (0 , driver .getConnectionCount ());
71+
4672 Connection firstConnection = DriverManager .getConnection (jdbcURL .build ());
4773 Assertions .assertTrue (firstConnection .isValid (5000 ));
74+ Assertions .assertEquals (1 , driver .getConnectionCount ());
4875
4976 YdbConnection first = firstConnection .unwrap (YdbConnection .class );
5077 Assertions .assertNotNull (first .getCtx ());
@@ -53,6 +80,7 @@ public void testContextCache() throws SQLException {
5380
5481 try (Connection conn2 = DriverManager .getConnection (jdbcURL .build ())) {
5582 Assertions .assertTrue (conn2 .isValid (5000 ));
83+ Assertions .assertEquals (1 , driver .getConnectionCount ());
5684
5785 YdbConnection unwrapped = conn2 .unwrap (YdbConnection .class );
5886 Assertions .assertNotNull (unwrapped .getCtx ());
@@ -62,6 +90,7 @@ public void testContextCache() throws SQLException {
6290 Properties props = new Properties ();
6391 try (Connection conn3 = DriverManager .getConnection (jdbcURL .build (), props )) {
6492 Assertions .assertTrue (conn3 .isValid (5000 ));
93+ Assertions .assertEquals (1 , driver .getConnectionCount ());
6594
6695 YdbConnection unwrapped = conn3 .unwrap (YdbConnection .class );
6796 Assertions .assertNotNull (unwrapped .getCtx ());
@@ -71,6 +100,7 @@ public void testContextCache() throws SQLException {
71100 props .setProperty ("TEST_KEY" , "TEST_VALUE" );
72101 try (Connection conn4 = DriverManager .getConnection (jdbcURL .build (), props )) {
73102 Assertions .assertTrue (conn4 .isValid (5000 ));
103+ Assertions .assertEquals (2 , driver .getConnectionCount ());
74104
75105 YdbConnection unwrapped = conn4 .unwrap (YdbConnection .class );
76106 Assertions .assertNotNull (unwrapped .getCtx ());
@@ -79,21 +109,64 @@ public void testContextCache() throws SQLException {
79109
80110 try (Connection conn5 = DriverManager .getConnection (jdbcURL .withArg ("test" , "false" ).build ())) {
81111 Assertions .assertTrue (conn5 .isValid (5000 ));
112+ Assertions .assertEquals (2 , driver .getConnectionCount ());
82113
83114 YdbConnection unwrapped = conn5 .unwrap (YdbConnection .class );
84115 Assertions .assertNotNull (unwrapped .getCtx ());
85116 Assertions .assertNotSame (ctx , unwrapped .getCtx ());
86117 }
87118
88119 firstConnection .close ();
120+ Assertions .assertEquals (0 , driver .getConnectionCount ());
89121
90122 try (Connection conn6 = DriverManager .getConnection (jdbcURL .build ())) {
91123 Assertions .assertTrue (conn6 .isValid (5000 ));
124+ Assertions .assertEquals (1 , driver .getConnectionCount ());
92125
93126 YdbConnection unwrapped = conn6 .unwrap (YdbConnection .class );
94127 Assertions .assertNotNull (unwrapped .getCtx ());
95128 Assertions .assertNotSame (ctx , unwrapped .getCtx ());
96129 }
130+
131+ Assertions .assertEquals (0 , driver .getConnectionCount ());
132+ }
133+
134+ @ Test
135+ public void testContextCacheDisable () throws SQLException {
136+ Driver jdbcDriver = DriverManager .getDriver (jdbcURL .build ());
137+ Assertions .assertTrue (jdbcDriver instanceof YdbDriver );
138+ YdbDriver driver = (YdbDriver )jdbcDriver ;
139+ Assertions .assertEquals (0 , driver .getConnectionCount ());
140+
141+ Properties props = new Properties ();
142+ props .put ("cacheConnectionsInDriver" , "false" );
143+
144+ Connection conn1 = DriverManager .getConnection (jdbcURL .build (), props );
145+ Assertions .assertEquals (0 , driver .getConnectionCount ());
146+
147+ YdbConnection first = conn1 .unwrap (YdbConnection .class );
148+ Assertions .assertNotNull (first .getCtx ());
149+ YdbContext ctx = first .getCtx ();
150+
151+ try (Connection conn2 = DriverManager .getConnection (jdbcURL .build ())) {
152+ Assertions .assertEquals (1 , driver .getConnectionCount ());
153+
154+ YdbConnection second = conn2 .unwrap (YdbConnection .class );
155+ Assertions .assertNotNull (second .getCtx ());
156+ Assertions .assertNotSame (ctx , second .getCtx ());
157+ }
158+
159+ Assertions .assertEquals (0 , driver .getConnectionCount ());
160+
161+ try (Connection conn2 = DriverManager .getConnection (jdbcURL .build (), props )) {
162+ Assertions .assertEquals (0 , driver .getConnectionCount ());
163+
164+ YdbConnection second = conn2 .unwrap (YdbConnection .class );
165+ Assertions .assertNotNull (second .getCtx ());
166+ Assertions .assertNotSame (ctx , second .getCtx ());
167+ }
168+
169+ conn1 .close ();
97170 }
98171
99172 @ Test
0 commit comments