org.rimudb.C3P0PoolTests.java Source code

Java tutorial

Introduction

Here is the source code for org.rimudb.C3P0PoolTests.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.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.sql.*;

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

/**
 * This testcase checks the pool functions correctly.
 * 
 * @author homeadmin
 *
 */
public class C3P0PoolTests {
    public final static Log log = LogFactory.getLog(C3P0PoolTests.class);

    @Test
    public void testCheckout() throws Exception {
        // Connect to the database
        CompoundDatabase cdb = new CompoundDatabase("/testconfig/pooltests-c3p0-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();

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

        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", 29, 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", 30, stat.getIdleConnections());

        cdb.disconnectAllNoException();
    }

    @Test
    public void testMultipleCheckout() throws Exception {
        // Connect to the database
        CompoundDatabase cdb = new CompoundDatabase("/testconfig/pooltests-c3p0-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", 29 - x, 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());
            assertEquals("Incorrect number of idle connections in pool", x + 11, stat.getIdleConnections());
        }

        cdb.disconnectAllNoException();
    }

    @Test
    public void testConnectionAttributes() throws Exception {
        // Connect to the database
        CompoundDatabase cdb = new CompoundDatabase("/testconfig/pooltests-c3p0-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();
    }
}