1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.jdbc;
17
18 import static org.junit.jupiter.api.Assertions.assertEquals;
19 import static org.junit.jupiter.api.Assertions.assertNotNull;
20
21 import java.sql.Connection;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Properties;
25
26 import org.apache.ibatis.BaseDataTest;
27 import org.apache.ibatis.datasource.pooled.PooledDataSource;
28 import org.junit.jupiter.api.Test;
29
30 class PooledDataSourceTest extends BaseDataTest {
31
32 @Test
33 void shouldProperlyMaintainPoolOf3ActiveAnd2IdleConnections() throws Exception {
34 PooledDataSource ds = createPooledDataSource(JPETSTORE_PROPERTIES);
35 try {
36 runScript(ds, JPETSTORE_DDL);
37 ds.setDefaultAutoCommit(false);
38 ds.setDriverProperties(new Properties() {
39 private static final long serialVersionUID = 1L;
40 {
41 setProperty("username", "sa");
42 setProperty("password", "");
43 }
44 });
45 ds.setPoolMaximumActiveConnections(3);
46 ds.setPoolMaximumIdleConnections(2);
47 ds.setPoolMaximumCheckoutTime(10000);
48 ds.setPoolPingConnectionsNotUsedFor(1);
49 ds.setPoolPingEnabled(true);
50 ds.setPoolPingQuery("SELECT * FROM PRODUCT");
51 ds.setPoolTimeToWait(10000);
52 ds.setLogWriter(null);
53 List<Connection> connections = new ArrayList<>();
54 for (int i = 0; i < 3; i++) {
55 connections.add(ds.getConnection());
56 }
57 assertEquals(3, ds.getPoolState().getActiveConnectionCount());
58 for (Connection c : connections) {
59 c.close();
60 }
61 assertEquals(2, ds.getPoolState().getIdleConnectionCount());
62 assertEquals(4, ds.getPoolState().getRequestCount());
63 assertEquals(0, ds.getPoolState().getBadConnectionCount());
64 assertEquals(0, ds.getPoolState().getHadToWaitCount());
65 assertEquals(0, ds.getPoolState().getAverageOverdueCheckoutTime());
66 assertEquals(0, ds.getPoolState().getClaimedOverdueConnectionCount());
67 assertEquals(0, ds.getPoolState().getAverageWaitTime());
68 assertNotNull(ds.getPoolState().toString());
69 } finally {
70 ds.forceCloseAll();
71 }
72 }
73
74 @Test
75 void shouldNotFailCallingToStringOverAnInvalidConnection() throws Exception {
76 PooledDataSource ds = createPooledDataSource(JPETSTORE_PROPERTIES);
77 Connection c = ds.getConnection();
78 c.close();
79 c.toString();
80 }
81
82 @Test
83 void ShouldReturnRealConnection() throws Exception {
84 PooledDataSource ds = createPooledDataSource(JPETSTORE_PROPERTIES);
85 Connection c = ds.getConnection();
86 PooledDataSource.unwrapConnection(c);
87 c.close();
88 }
89
90 }