33import lombok .AllArgsConstructor ;
44import lombok .extern .log4j .Log4j2 ;
55import me .zort .sqllib .SQLConnectionBuilder ;
6+ import me .zort .sqllib .SQLConnectionPool ;
67import me .zort .sqllib .SQLDatabaseConnection ;
78import me .zort .sqllib .SQLDatabaseOptions ;
89import me .zort .sqllib .api .data .QueryResult ;
1617import org .junit .jupiter .api .condition .EnabledOnOs ;
1718import org .junit .jupiter .api .condition .OS ;
1819
20+ import java .sql .SQLException ;
1921import java .util .Optional ;
2022
2123import static org .junit .jupiter .api .Assertions .*;
2729public class TestCase1 { // Basic operations
2830
2931 private SQLDatabaseConnection connection ;
32+ private SQLConnectionBuilder builder ;
3033 private static final String TABLE_NAME = "users" ;
3134 private final User user1 = new User ("User1" , 100 );
3235 private final User user2 = new User ("User2" , 200 );
@@ -51,8 +54,8 @@ public void prepare() {
5154
5255 DefaultSQLEndpoint endpoint = new DefaultSQLEndpoint (String .format ("%s:3306" , host ), "test" , "test" , "test" );
5356
54- connection = SQLConnectionBuilder .of (endpoint )
55- .withDriver ("com.mysql.cj.jdbc.Driver" )
57+ connection = ( builder = SQLConnectionBuilder .of (endpoint )
58+ .withDriver ("com.mysql.cj.jdbc.Driver" ))
5659 .build (options );
5760
5861 System .out .println ("Connection prepared, connecting..." );
@@ -147,9 +150,29 @@ public void test5_Delete() {
147150 assertNull (result .getRejectMessage ());
148151 }
149152
153+ @ Timeout (10 )
154+ @ Test
155+ public void test6_Pool () {
156+ SQLConnectionPool .Options options = new SQLConnectionPool .Options ();
157+ options .setMaxConnections (10 );
158+ options .setBorrowObjectTimeout (5000L );
159+ options .setBlockWhenExhausted (false );
160+ SQLConnectionPool pool = new SQLConnectionPool (builder , options );
161+ try (SQLConnectionPool .Resource resource = pool .getResource ()) {
162+ System .out .println ("Got connection from pool" );
163+ SQLDatabaseConnection connection = resource .getConnection ();
164+ assertTrue (connection .save (TABLE_NAME , user1 ).isSuccessful ());
165+ } catch (SQLException e ) {
166+ throw new RuntimeException (e );
167+ }
168+ assertEquals (1 , pool .size ());
169+ pool .close ();
170+ assertEquals (0 , pool .size ());
171+ }
172+
150173 @ Timeout (5 )
151174 @ Test
152- public void test6_Close () {
175+ public void test7_Close () {
153176 System .out .println ("Closing connection..." );
154177 connection .disconnect ();
155178 System .out .println ("Connection closed" );
0 commit comments