import org.postgresql.jdbc3.Jdbc3PoolingDataSource; import java.sql.*; public class test29 { static Jdbc3PoolingDataSource loPoolSource = null; private Connection loConnection = null; private static final String DATABASE_POOL_DATA_SOURCE_NAME = "test"; private static final String DATABASE_POOL_SERVER_NAME = "localhost"; private static final int DATABASE_POOL_SERVER_PORT = 5432; private static final String DATABASE_POOL_DATABASE_NAME = "test"; private static final String DATABASE_POOL_USER_NAME = "test"; private static final String DATABASE_POOL_PASSWORD = "test"; private static final int DATABASE_POOL_MAXCONNECTIONS = 30; test29() { } private static void setPool() { // DataSource initialization System.out.println("setPool()"); try { System.out.println("setting pool"); // constructs a pool only when loPoolSource is not assigned. // this will prevent problems if the same pool is set more than // one time if (loPoolSource == null) { loPoolSource = new Jdbc3PoolingDataSource(); // DataSource configuration. loPoolSource.setDataSourceName(DATABASE_POOL_DATA_SOURCE_NAME); // any name is valid here loPoolSource.setServerName(DATABASE_POOL_SERVER_NAME); loPoolSource.setPortNumber(DATABASE_POOL_SERVER_PORT); loPoolSource.setDatabaseName(DATABASE_POOL_DATABASE_NAME); loPoolSource.setUser(DATABASE_POOL_USER_NAME); loPoolSource.setPassword(DATABASE_POOL_PASSWORD); loPoolSource.setMaxConnections(DATABASE_POOL_MAXCONNECTIONS); } else { System.out.println("pool is set"); return; } } catch (Exception loException) { // logger.logToFile(loException); loException.printStackTrace(); } } public Statement openConnection() { Statement loStatement; System.out.println("opening con"); try { // gets a connection from Pool this.loConnection = loPoolSource.getConnection(); System.out.println("loConnection = " + this.loConnection); // loStatement = this.loConnection.createStatement(); // System.out.println("Statement = " + loStatement); // return loStatement; return this.loConnection.createStatement(); } catch (Exception loException) { loException.printStackTrace(); } return null; } public void closeConnection() { System.out.println("closing con"); try { if (this.loConnection != null) this.loConnection.close(); } catch (Exception loException) { loException.printStackTrace(); } } public static void main(String args[]) { Statement loStatement = null; test29 loPoolConnection = new test29(); loPoolConnection.setPool(); loStatement = loPoolConnection.openConnection(); ResultSet lors = null; try { lors = loStatement.executeQuery("select * from test"); while (lors.next()) { System.out.println(lors.getString(1)); } } catch (Exception e) { e.printStackTrace(); } finally { try { lors.close(); loStatement.close(); loPoolConnection.closeConnection(); } catch (Exception loException) { loException.printStackTrace(); } } loPoolConnection = new test29(); loPoolConnection.setPool(); loStatement = null; loStatement = loPoolConnection.openConnection(); lors = null; try { lors = loStatement.executeQuery("select * from test"); while (lors.next()) { System.out.println(lors.getString(2)); } } catch (Exception e) { e.printStackTrace(); } finally { try { lors.close(); loStatement.close(); loPoolConnection.closeConnection(); } catch (Exception loException) { loException.printStackTrace(); } } } }