// Test program for the MiniConnectionPoolManager class. import java.io.PrintWriter; import java.lang.Thread; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; import java.util.Random; import javax.sql.ConnectionPoolDataSource; import biz.source_code.miniConnectionPoolManager.MiniConnectionPoolManager; public class TestMiniConnectionPoolManager { private static final int maxConnections = 8; // number of connections private static final int noOfThreads = 50; // number of worker threads private static final int processingTime = 30; // total processing time of the test program in seconds private static final int threadPauseTime1 = 100; // max. thread pause time in microseconds, without a connection private static final int threadPauseTime2 = 100; // max. thread pause time in microseconds, with a connection private static MiniConnectionPoolManager poolMgr; private static WorkerThread[] threads; private static boolean shutdownFlag; private static Object shutdownObj = new Object(); private static Random random = new Random(); private static class WorkerThread extends Thread { public int threadNo; public void run() {threadMain (threadNo); }}; private static ConnectionPoolDataSource createDataSource() throws Exception { // Version for H2: org.h2.jdbcx.JdbcDataSource dataSource = new org.h2.jdbcx.JdbcDataSource(); dataSource.setURL ("jdbc:h2:file:c:/temp/temp_TestMiniConnectionPoolManagerDB;DB_CLOSE_DELAY=-1"); // Version for Apache Derby: /* org.apache.derby.jdbc.EmbeddedConnectionPoolDataSource dataSource = new org.apache.derby.jdbc.EmbeddedConnectionPoolDataSource(); dataSource.setDatabaseName ("c:/temp/temp_TestMiniConnectionPoolManagerDB"); dataSource.setCreateDatabase ("create"); dataSource.setLogWriter (new PrintWriter(System.out)); */ // Version for JTDS: /* net.sourceforge.jtds.jdbcx.JtdsDataSource dataSource = new net.sourceforge.jtds.jdbcx.JtdsDataSource(); dataSource.setAppName ("TestMiniConnectionPoolManager"); dataSource.setDatabaseName ("Northwind"); dataSource.setServerName ("localhost"); dataSource.setUser ("sa"); dataSource.setPassword (System.getProperty("saPassword")); */ // Version for the Microsoft SQL Server driver (sqljdbc.jar): /* // The sqljdbc 1.1 documentation, chapter "Using Connection Pooling", recommends to use // SQLServerXADataSource instead of SQLServerConnectionPoolDataSource, even when no // distributed transactions are used. com.microsoft.sqlserver.jdbc.SQLServerXADataSource dataSource = new com.microsoft.sqlserver.jdbc.SQLServerXADataSource(); dataSource.setApplicationName ("TestMiniConnectionPoolManager"); dataSource.setDatabaseName ("Northwind"); dataSource.setServerName ("localhost"); dataSource.setUser ("sa"); dataSource.setPassword (System.getProperty("saPassword")); dataSource.setLogWriter (new PrintWriter(System.out)); */ // Version for Oracle: /* oracle.jdbc.pool.OracleConnectionPoolDataSource dataSource = new oracle.jdbc.pool.OracleConnectionPoolDataSource(); dataSource.setDriverType ("thin"); dataSource.setServerName ("vm1"); dataSource.setPortNumber (1521); dataSource.setServiceName ("vm1.inventec.ch"); dataSource.setUser ("system"); dataSource.setPassword ("x"); */ return dataSource; } public static void main (String[] args) throws Exception { System.out.println ("Program started."); ConnectionPoolDataSource dataSource = createDataSource(); poolMgr = new MiniConnectionPoolManager(dataSource,maxConnections); initDb(); startWorkerThreads(); pause (processingTime*1000000); System.out.println ("\nStopping threads."); stopWorkerThreads(); System.out.println ("\nAll threads stopped."); poolMgr.dispose(); System.out.println ("Program completed."); } private static void startWorkerThreads() { threads = new WorkerThread[noOfThreads]; for (int threadNo=0; threadNo