Connecting to database is very common in most of the project and if number of connection to database is high it better to have database connection pooling.This asset contains code sample for creating connection during run time.
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Time; import java.util.Date; import java.util.Hashtable; import java.util.Properties; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.ConnectionPoolDataSource; import javax.sql.DataSource; import javax.sql.PooledConnection; import oracle.jdbc.pool.OracleConnectionCacheImpl; import oracle.jdbc.pool.OracleConnectionPoolDataSource; import org.apache.commons.dbcp.AbandonedConfig; import org.apache.commons.dbcp.ConnectionFactory; import org.apache.commons.dbcp.DriverManagerConnectionFactory; import org.apache.commons.dbcp.PoolableConnectionFactory; import org.apache.commons.dbcp.PoolingDataSource; import org.apache.commons.dbcp.PoolingDriver; import org.apache.commons.pool.impl.GenericObjectPool; import weblogic.jndi.Environment; import weblogic.management.MBeanHome; import weblogic.management.configuration.JDBCConnectionPoolMBean; import weblogic.management.configuration.JDBCTxDataSourceMBean; import weblogic.management.configuration.ServerMBean; import weblogic.t3.srvr.ServerRuntime; /** * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ public class PoolDemo { public static GenericObjectPool pool ; public static OracleConnectionCacheImpl iotConPool; public static void main(String args[]) throws Exception { try { // get the connection using dpcp connection pooling mach DataSource ds =getDpCpPool("oracle.jdbc.OracleDriver","jdbc:oracle:thin:@suznd264.qintra.com:1563:uesott03","esot","esot"); for (int i =0; i<8; i++){ System.out.println(ds.getConnection()); System.out.println(" pool no"+ pool.getNumActive()); } // get the connection using oracle pooling System.out.println(getOraclePoolConnection("oracle.jdbc.OracleDriver","jdbc:oracle:thin:@suznd264.qintra.com:1563:uesott03","esot","esot")); } catch (Throwable e) { e.printStackTrace(); // a failure occurred log message; } finally { } } public static DataSource getDpCpPool(String driver, String connectURI, String username, String password) { PoolingDataSource dataSource = null; try { Class.forName(driver); // // First, we'll need a ObjectPool that serves as the // actual pool of connections. // // We'll use a GenericObjectPool instance, although // any ObjectPool implementation will suffice. // // // Next, we'll create a ConnectionFactory that the // pool will use to create Connections. // We'll use the DriverManagerConnectionFactory, // using the connect string passed in the command line // arguments. // ConnectionFactory connectionFactory = new DriverManagerConnectionFactory( connectURI, username, password); // // Now we'll create the PoolableConnectionFactory, which wraps // the "real" Connections created by the ConnectionFactory with // the classes that implement the pooling functionality. // pool = new GenericObjectPool( null, // PoolableObjectFactory, can be null 5, // max active GenericObjectPool.WHEN_EXHAUSTED_BLOCK, // action when exhausted 3000, // max wait (milli seconds) 2, // max idle false, // test on borrow false // test on return ); // create a PoolableConnectionFactory AbandonedConfig abandonedConfig = new AbandonedConfig(); // flag to remove abandoned connections from pool abandonedConfig.setRemoveAbandoned(true); // timeout (seconds) before removing abandoned connections abandonedConfig.setRemoveAbandonedTimeout(30); // Flag to log stack traces for application code which abandoned a // Statement or Connection abandonedConfig.setLogAbandoned(true); PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory( // the connection factory connectionFactory, // the object pool pool, // statement pool factory for pooling prepared statements, // or null for no pooling null, // validation query (must return at least 1 row e.g. Oracle: // select count(*) from dual) to test connection, can be // null null, // default "read only" setting for borrowed connections false, // default "auto commit" setting for returned connections true, // AbandonedConfig object configures how to handle abandoned // connections abandonedConfig ); dataSource = new PoolingDataSource(pool); return dataSource ; } catch (Throwable e) { e.printStackTrace(); } finally { } return null; } public static Connection getOraclePoolConnection(String driver, String connectURI, String username, String password) { try { if (iotConPool == null) { Class.forName(driver); // Create a OracleConnectionPoolDataSource instance OracleConnectionPoolDataSource ocpds = new OracleConnectionPoolDataSource(); // Set connection parameters ocpds.setURL(connectURI); ocpds.setUser(username); ocpds.setPassword(password); // Associate it with the Cache iotConPool = new OracleConnectionCacheImpl(ocpds); // Set the Max Limit iotConPool.setMinLimit(10); // Set the Scheme // ods.setCacheScheme(OracleConnectionCacheImpl.FIXED_RETURN_NULL_SCHEME); } return iotConPool.getConnection(); } catch (Throwable e) { e.printStackTrace(); } finally { } return null; } }