View Javadoc
1   /*
2    *    Copyright 2009-2023 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.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  }