Search in sources :

Example 1 with DalTomcatDataSource

use of com.ctrip.platform.dal.dao.datasource.tomcat.DalTomcatDataSource in project dal by ctripcorp.

the class DalConnectionPoolTest method simpleTest.

/**
 * need local mysql
 *
 * @throws SQLException
 */
@Test
public void simpleTest() throws SQLException {
    int initSize = 5;
    PoolConfiguration p = new PoolProperties();
    p.setUrl("jdbc:mysql://localhost:3306/test?a=1,b=2");
    p.setDriverClassName("com.mysql.jdbc.Driver");
    p.setUsername("root");
    p.setPassword("root");
    p.setInitialSize(initSize);
    final AtomicInteger create = new AtomicInteger();
    final AtomicInteger release = new AtomicInteger();
    final AtomicInteger abandon = new AtomicInteger();
    DalConnectionPool.setConnectionListener(new AbstractConnectionListener() {

        @Override
        public void doOnCreateConnection(String poolDesc, Connection connection) {
            create.incrementAndGet();
        }

        @Override
        public void doOnReleaseConnection(String poolDesc, Connection connection) {
            release.incrementAndGet();
        }

        @Override
        protected void doOnAbandonConnection(String poolDesc, Connection connection) {
            abandon.incrementAndGet();
        }

        @Override
        public int getOrder() {
            return 0;
        }
    });
    DataSource dataSource = new DalTomcatDataSource(p);
    ConnectionPool dalConnectionPool = dataSource.createPool();
    Assert.assertEquals(initSize, create.get());
    List<Connection> connections = new LinkedList<>();
    for (int i = 0; i < initSize; i++) {
        connections.add(dalConnectionPool.getConnection());
    }
    Assert.assertEquals(initSize, create.get());
    for (int i = 0; i < initSize; i++) {
        connections.add(dalConnectionPool.getConnection());
    }
    Assert.assertEquals(initSize * 2, create.get());
    for (Connection connection : connections) {
        connection.close();
    }
    Assert.assertEquals(initSize * 2, create.get());
    Assert.assertEquals(0, release.get());
    dalConnectionPool.purge();
    Assert.assertEquals(initSize * 2, create.get());
    Assert.assertEquals(initSize * 2, release.get());
}
Also used : ConnectionPool(org.apache.tomcat.jdbc.pool.ConnectionPool) DalConnectionPool(com.ctrip.platform.dal.dao.datasource.tomcat.DalConnectionPool) PoolConfiguration(org.apache.tomcat.jdbc.pool.PoolConfiguration) Connection(java.sql.Connection) AbstractConnectionListener(com.ctrip.platform.dal.dao.datasource.AbstractConnectionListener) LinkedList(java.util.LinkedList) PoolProperties(org.apache.tomcat.jdbc.pool.PoolProperties) DataSource(org.apache.tomcat.jdbc.pool.DataSource) DalTomcatDataSource(com.ctrip.platform.dal.dao.datasource.tomcat.DalTomcatDataSource) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DalTomcatDataSource(com.ctrip.platform.dal.dao.datasource.tomcat.DalTomcatDataSource) Test(org.junit.Test)

Aggregations

AbstractConnectionListener (com.ctrip.platform.dal.dao.datasource.AbstractConnectionListener)1 DalConnectionPool (com.ctrip.platform.dal.dao.datasource.tomcat.DalConnectionPool)1 DalTomcatDataSource (com.ctrip.platform.dal.dao.datasource.tomcat.DalTomcatDataSource)1 Connection (java.sql.Connection)1 LinkedList (java.util.LinkedList)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ConnectionPool (org.apache.tomcat.jdbc.pool.ConnectionPool)1 DataSource (org.apache.tomcat.jdbc.pool.DataSource)1 PoolConfiguration (org.apache.tomcat.jdbc.pool.PoolConfiguration)1 PoolProperties (org.apache.tomcat.jdbc.pool.PoolProperties)1 Test (org.junit.Test)1