View Javadoc
1   /*
2    *    Copyright 2009-2021 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    *       http://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.*;
19  
20  import java.sql.Connection;
21  import java.util.ArrayList;
22  import java.util.List;
23  import java.util.Properties;
24  
25  import org.apache.ibatis.BaseDataTest;
26  import org.apache.ibatis.datasource.pooled.PooledDataSource;
27  import org.hsqldb.jdbc.JDBCConnection;
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          {
40            setProperty("username", "sa");
41            setProperty("password", "");
42          }
43        });
44        ds.setPoolMaximumActiveConnections(3);
45        ds.setPoolMaximumIdleConnections(2);
46        ds.setPoolMaximumCheckoutTime(10000);
47        ds.setPoolPingConnectionsNotUsedFor(1);
48        ds.setPoolPingEnabled(true);
49        ds.setPoolPingQuery("SELECT * FROM PRODUCT");
50        ds.setPoolTimeToWait(10000);
51        ds.setLogWriter(null);
52        List<Connection> connections = new ArrayList<>();
53        for (int i = 0; i < 3; i++) {
54          connections.add(ds.getConnection());
55        }
56        assertEquals(3, ds.getPoolState().getActiveConnectionCount());
57        for (Connection c : connections) {
58          c.close();
59        }
60        assertEquals(2, ds.getPoolState().getIdleConnectionCount());
61        assertEquals(4, ds.getPoolState().getRequestCount());
62        assertEquals(0, ds.getPoolState().getBadConnectionCount());
63        assertEquals(0, ds.getPoolState().getHadToWaitCount());
64        assertEquals(0, ds.getPoolState().getAverageOverdueCheckoutTime());
65        assertEquals(0, ds.getPoolState().getClaimedOverdueConnectionCount());
66        assertEquals(0, ds.getPoolState().getAverageWaitTime());
67        assertNotNull(ds.getPoolState().toString());
68      } finally {
69        ds.forceCloseAll();
70      }
71    }
72  
73    @Test
74    void shouldNotFailCallingToStringOverAnInvalidConnection() throws Exception {
75      PooledDataSource ds = createPooledDataSource(JPETSTORE_PROPERTIES);
76      Connection c = ds.getConnection();
77      c.close();
78      c.toString();
79    }
80  
81    @Test
82    void ShouldReturnRealConnection() throws Exception {
83      PooledDataSource ds = createPooledDataSource(JPETSTORE_PROPERTIES);
84      Connection c = ds.getConnection();
85      JDBCConnection realConnection = (JDBCConnection) PooledDataSource.unwrapConnection(c);
86      c.close();
87    }
88  }