View Javadoc
1   /**
2    *    Copyright 2009-2015 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 org.apache.ibatis.BaseDataTest;
19  import org.apache.ibatis.datasource.pooled.PooledDataSource;
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertNotNull;
22  
23  import org.hsqldb.jdbc.JDBCConnection;
24  import org.junit.Test;
25  
26  import java.sql.Connection;
27  import java.util.ArrayList;
28  import java.util.List;
29  import java.util.Properties;
30  
31  public class PooledDataSourceTest extends BaseDataTest {
32  
33    @Test
34    public 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          {
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<Connection>();
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    public 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    public void ShouldReturnRealConnection() throws Exception {
84      PooledDataSource ds = createPooledDataSource(JPETSTORE_PROPERTIES);
85      Connection c = ds.getConnection();
86      JDBCConnection realConnection = (JDBCConnection) PooledDataSource.unwrapConnection(c);
87    }
88  }