JDBC URL Format
Sample Code for OCI Connection Pooling
The following code illustrates the use of OCI connection pooling in a sample
application:
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import oracle.jdbc.OracleDriver;
import oracle.jdbc.pool.OracleOCIConnectionPool;
public class conPoolAppl extends Thread
{
public static final String query = "SELECT object_name FROM all_objects WHERE
rownum < 300";
static public void main(String args[]) throws SQLException
{
int _maxCount = 10;
Connection []conn = new Connection[_maxCount];
try
{
DriverManager.registerDriver(new OracleDriver());
String s = null; //System.getProperty ("JDBC_URL");
//String url = ( s == null ? "jdbc:oracle:oci8:@orcl" : s);
String url = "jdbc:oracle:oci8:@orcl.rmmslang.com";
OracleOCIConnectionPool cpool = new OracleOCIConnectionPool("scott",
"tiger", url, null);
// Print out the default configuration for the OracleOCIConnectionPool
System.out.println ("-- The default configuration for the
OracleOCIConnectionPool --");
displayPoolConfig(cpool);
//Set up the initial pool configuration
Properties p1 = new Properties();
p1.put (OracleOCIConnectionPool.CONNPOOL_MIN_LIMIT, Integer.toString(1));
p1.put (OracleOCIConnectionPool.CONNPOOL_MAX_LIMIT,
Integer.toString(_maxCount));
p1.put (OracleOCIConnectionPool.CONNPOOL_INCREMENT, Integer.toString(1));
// Enable the initial configuration
cpool.setPoolConfig(p1);
Thread []t = new Thread[_maxCount];
for (int i = 0; i < _maxCount; ++i)
{
conn[i] = cpool.getConnection("scott", "tiger");
if ( conn[i] == null )
{
System.out.println("Unable to create connection.");
return;
}
t[i] = new conPoolAppl (i, conn[i]);
t[i].start ();
//displayPoolConfig(cpool);
}
((conPoolAppl)t[0]).startAllThreads ();
try
{
Thread.sleep (200);
}
catch (Exception ea) {}
displayPoolConfig(cpool);
for (int i = 0; i < _maxCount; ++i)
t[i].join ();
}
catch(Exception ex)
{
System.out.println("Error: " + ex);
ex.printStackTrace ();
return;
}
finally
{
for (int i = 0; i < _maxCount; ++i)
if (conn[i] != null)
conn[i].close ();
}
} //end of main
private Connection m_conn;
private static boolean m_startThread = false;
private int m_threadId;
public conPoolAppl (int i, Connection conn)
{
m_threadId = i;
m_conn = conn;
}
public void startAllThreads ()
{
m_startThread = true;
}
public void run ()
{
while (!m_startThread) Thread.yield ();
try
{
doQuery (m_conn);
}
catch (SQLException ea)
{
System.out.println ("*** Thread id: " + m_threadId);
ea.printStackTrace ();
}
} // end of run
private static void doQuery (Connection conn) throws SQLException
{
PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
pstmt = conn.prepareStatement (query);
rs = pstmt.executeQuery ();
while (rs.next ())
{
//System.out.println ("Object name: " +rs.getString (1));
}
}
catch (Exception ea)
{
System.out.println ("Error during execution: " +ea);
ea.printStackTrace ();
}
finally
{
if (rs != null)
rs.close ();
if (pstmt != null)
pstmt.close ();
if (conn != null)
conn.close ();
}
} // end of doQuery (Connection)
// Display the current status of the OracleOCIConnectionPool
private static void displayPoolConfig (OracleOCIConnectionPool cpool) throws
SQLException
{
System.out.println (" Min poolsize Limit: " + cpool.getMinLimit());
System.out.println (" Max poolsize Limit: " + cpool.getMaxLimit());
/*
System.out.println (" Connection Increment: " +
cpool.getConnectionIncrement());
System.out.println (" NoWait: " + cpool.getNoWait());
System.out.println (" Timeout: " + cpool.getTimeout());
*/
System.out.println (" PoolSize: " + cpool.getPoolSize());
System.out.println (" ActiveSize: " + cpool.getActiveSize());
}
} // end of class conPoolAppl