org.rimudb.DBCPPoolTests.java Source code

Java tutorial

Introduction

Here is the source code for org.rimudb.DBCPPoolTests.java

Source

/*
 * Copyright (c) 2008-2011 Simon Ritchie.
 * All rights reserved. 
 * 
 * This program is free software: you can redistribute it and/or modify 
 * it under the terms of the GNU Lesser General Public License as published 
 * by the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 * See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this program.  If not, see http://www.gnu.org/licenses/>.
 */
package org.rimudb;

import static org.junit.Assert.*;

import java.sql.*;

import org.apache.commons.logging.*;
import org.junit.*;
import org.rimudb.pool.*;
import org.rimudb.testdb.*;

/**
 * @author Simon Ritchie
 *
 */
public class DBCPPoolTests {
    public final static Log log = LogFactory.getLog(DBCPPoolTests.class);

    @Test
    public void testCheckout() throws Exception {

        // Wait for the delete connection to close
        Thread.sleep(100);

        // Connect to the database
        CompoundDatabase cdb = new CompoundDatabase("/testconfig/pooltests-dbcp-1-jdbcconfig.xml", true);
        cdb.connect("dbid-1");

        // Create the table
        cdb.createTable(OrderTransactionDO.class, true);

        // Remove records from the table
        OrderTransactionFinder orderTransactionFinder = new OrderTransactionFinder(cdb);
        orderTransactionFinder.deleteAll();

        Database db = cdb.getDatabase("dbid-1");
        Connection databaseConnection = db.getDatabaseConnection();

        IConnectionManager connectionManager = db.getConnectionManager();
        PoolStatistic stat = connectionManager.getPoolStatistic();
        assertNotNull("PoolStatistics is null", stat);
        assertEquals("Incorrect number of active connections in pool", 1, stat.getActiveConnections());
        assertEquals("Incorrect number of idle connections in pool", 0, stat.getIdleConnections());

        databaseConnection.close();

        // Wait for the connection to close
        Thread.sleep(100);

        stat = connectionManager.getPoolStatistic();
        assertNotNull("PoolStatistics is null", stat);
        assertEquals("Incorrect number of active connections in pool", 0, stat.getActiveConnections());
        assertEquals("Incorrect number of idle connections in pool", 1, stat.getIdleConnections());

        cdb.disconnectAllNoException();
    }

    @Test
    public void testMultipleCheckout() throws Exception {
        // Connect to the database
        CompoundDatabase cdb = new CompoundDatabase("/testconfig/pooltests-dbcp-1-jdbcconfig.xml", true);
        cdb.connect("dbid-1");

        // Create the table
        cdb.createTable(OrderTransactionDO.class, true);

        // Remove records from the table
        OrderTransactionFinder orderTransactionFinder = new OrderTransactionFinder(cdb);
        orderTransactionFinder.deleteAll();

        Database db = cdb.getDatabase("dbid-1");
        IConnectionManager connectionManager = db.getConnectionManager();

        PoolStatistic stat = null;

        Connection databaseConnections[] = new Connection[20];

        // Wait up to 1 second for the previous connection(s) to close 
        for (int x = 0; x < 10; x++) {
            int activeConnections = connectionManager.getPoolStatistic().getActiveConnections();
            if (activeConnections == 0) {
                break;
            }
            Thread.sleep(100);
        }

        int activeConnections = connectionManager.getPoolStatistic().getActiveConnections();
        log.info("Found " + activeConnections + " active connections at start of testMultipleCheckout()");

        // Open 20 connections
        for (int x = 0; x < 20; x++) {
            databaseConnections[x] = db.getDatabaseConnection();
            stat = connectionManager.getPoolStatistic();
            assertNotNull("PoolStatistics is null", stat);
            assertEquals("Incorrect number of active connections in pool", x + 1, stat.getActiveConnections());
            assertEquals("Incorrect number of idle connections in pool", 0, stat.getIdleConnections());
        }

        // Now close the connections
        for (int x = 0; x < 20; x++) {
            databaseConnections[x].close();
            Thread.sleep(50); // Wait to allow the connection to close before we count the connections

            stat = connectionManager.getPoolStatistic();

            assertNotNull("PoolStatistics is null", stat);
            assertEquals("Incorrect number of active connections in pool", 19 - x, stat.getActiveConnections());

            // max_idle_connections is set to 10
            if (x < 10) {
                assertEquals("Incorrect number of idle connections in pool", x + 1, stat.getIdleConnections());
            } else {
                assertEquals("Incorrect number of idle connections in pool", 10, stat.getIdleConnections());
            }
        }

        cdb.disconnectAllNoException();
    }

    @Test
    public void testConnectionAttributes() throws Exception {
        // Connect to the database
        CompoundDatabase cdb = new CompoundDatabase("/testconfig/pooltests-dbcp-2-jdbcconfig.xml", true);

        cdb.connect("dbid-1");
        Database db = cdb.getDatabase("dbid-1");
        Connection conn = db.getDatabaseConnection();
        assertEquals("Wrong transaction isolation", Connection.TRANSACTION_READ_COMMITTED,
                conn.getTransactionIsolation());
        assertEquals("Wrong auto commit", false, conn.getAutoCommit());
        db.disconnect();

        cdb.connect("dbid-2");
        db = cdb.getDatabase("dbid-2");
        conn = db.getDatabaseConnection();
        assertEquals("Wrong transaction isolation", Connection.TRANSACTION_READ_UNCOMMITTED,
                conn.getTransactionIsolation());
        assertEquals("Wrong auto commit", true, conn.getAutoCommit());
        db.disconnect();

        cdb.connect("dbid-3");
        db = cdb.getDatabase("dbid-3");
        conn = db.getDatabaseConnection();
        assertEquals("Wrong transaction isolation", Connection.TRANSACTION_REPEATABLE_READ,
                conn.getTransactionIsolation());
        assertEquals("Wrong auto commit", false, conn.getAutoCommit());
        db.disconnect();

        cdb.connect("dbid-4");
        db = cdb.getDatabase("dbid-4");
        conn = db.getDatabaseConnection();
        assertEquals("Wrong transaction isolation", Connection.TRANSACTION_SERIALIZABLE,
                conn.getTransactionIsolation());
        assertEquals("Wrong auto commit", true, conn.getAutoCommit());
        db.disconnect();

        cdb.disconnectAllNoException();
    }

}