View Javadoc
1   /*
2    *    Copyright 2009-2024 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.apache.ibatis.jdbc;
17  
18  import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertNotNull;
21  
22  import java.sql.Connection;
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.Properties;
26  
27  import org.apache.ibatis.BaseDataTest;
28  import org.apache.ibatis.datasource.pooled.PooledDataSource;
29  import org.junit.jupiter.api.Test;
30  
31  class PooledDataSourceTest extends BaseDataTest {
32  
33    @Test
34    void shouldProperlyMaintainPoolOf3ActiveAnd2IdleConnections() throws Exception {
35      PooledDataSource ds = createPooledDataSource(JPETSTORE_PROPERTIES);
36      try {
37        runScript(ds, JPETSTORE_DDL);
38        ds.setDefaultAutoCommit(false);
39        ds.setDriverProperties(new Properties() {
40          private static final long serialVersionUID = 1L;
41          {
42            setProperty("username", "sa");
43            setProperty("password", "");
44          }
45        });
46        ds.setPoolMaximumActiveConnections(3);
47        ds.setPoolMaximumIdleConnections(2);
48        ds.setPoolMaximumCheckoutTime(10000);
49        ds.setPoolPingConnectionsNotUsedFor(1);
50        ds.setPoolPingEnabled(true);
51        ds.setPoolPingQuery("SELECT * FROM PRODUCT");
52        ds.setPoolTimeToWait(10000);
53        ds.setLogWriter(null);
54        List<Connection> connections = new ArrayList<>();
55        for (int i = 0; i < 3; i++) {
56          connections.add(ds.getConnection());
57        }
58        assertEquals(3, ds.getPoolState().getActiveConnectionCount());
59        for (Connection c : connections) {
60          c.close();
61        }
62        assertEquals(2, ds.getPoolState().getIdleConnectionCount());
63        assertEquals(4, ds.getPoolState().getRequestCount());
64        assertEquals(0, ds.getPoolState().getBadConnectionCount());
65        assertEquals(0, ds.getPoolState().getHadToWaitCount());
66        assertEquals(0, ds.getPoolState().getAverageOverdueCheckoutTime());
67        assertEquals(0, ds.getPoolState().getClaimedOverdueConnectionCount());
68        assertEquals(0, ds.getPoolState().getAverageWaitTime());
69        assertNotNull(ds.getPoolState().toString());
70      } finally {
71        ds.forceCloseAll();
72      }
73    }
74  
75    @Test
76    void shouldNotFailCallingToStringOverAnInvalidConnection() {
77      assertDoesNotThrow(() -> {
78        PooledDataSource ds = createPooledDataSource(JPETSTORE_PROPERTIES);
79        Connection c = ds.getConnection();
80        c.close();
81        c.toString();
82      });
83    }
84  
85    @Test
86    void ShouldReturnRealConnection() {
87      assertDoesNotThrow(() -> {
88        PooledDataSource ds = createPooledDataSource(JPETSTORE_PROPERTIES);
89        Connection c = ds.getConnection();
90        PooledDataSource.unwrapConnection(c);
91        c.close();
92      });
93    }
94  
95  }