Tuesday, October 14, 2008

Oracle UCP : Hello World

" The Universal Connection Pool provides an enhanced common infrastructure for database connection pooling. It is usable within the OC4J context as well as by non-Java EE applications for connecting to Oracle and non-Oracle databases and other resources like JCA and LDAP connections. "


I am exploring Oracle Universal Connection Pool for a customer engagement. I was exploring the API & came up with a basic "Hello World" style introduction for the API.

I just wanted to share it here:-


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import oracle.ucp.UniversalConnectionPoolException;
import oracle.ucp.jdbc.PoolDataSource;
import oracle.ucp.jdbc.PoolDataSourceImpl;


public class UCPHelloWorld
{

public UCPHelloWorld()
{

}// end of constructor

public static void main(String args[])
{
String strConnectionPoolName= null;
String strConnectionFactory = null;
String strDatabaseURL = null;
String strDatabaseUser = null;
String strUserPassword = null;
String strSQLQuery = null;
String strEmployeeName = null;

Connection objConnection = null;
Statement objStatement = null;
ResultSet objResultSet = null;

PoolDataSource objPoolDataSource= null;

strConnectionPoolName = "myConnectionPool";
strConnectionFactory = "oracle.jdbc.pool.OracleDataSource";
strDatabaseURL = "jdbc:oracle:thin:@localhost:1521/XE";
strDatabaseUser = "scott";
strUserPassword = "tiger";
strSQLQuery = "SELECT ENAME FROM EMP";
strEmployeeName = "ENAME";


try
{
objPoolDataSource = new PoolDataSourceImpl();

// Basic Connection Parameters
objPoolDataSource.setConnectionFactoryClassName(strConnectionFactory);
objPoolDataSource.setURL(strDatabaseURL);
objPoolDataSource.setUser(strDatabaseUser);
objPoolDataSource.setPassword(strUserPassword);
objPoolDataSource.setConnectionPoolName(strConnectionPoolName);

// Set the Pool Size
objPoolDataSource.setMinPoolSize(4);
objPoolDataSource.setMaxPoolSize(10);
objPoolDataSource.setConnectionWaitTimeout(2);
objPoolDataSource.setInitialPoolSize(4);

System.out.println("Before getting the Connection");

objConnection = objPoolDataSource.getConnection();
objStatement = objConnection.createStatement();
objResultSet = objStatement.executeQuery(strSQLQuery);

System.out.println("After getting the Connection");

while(objResultSet.next())
{

System.out.println(objResultSet.getString(strEmployeeName));

}// end of resultset

objResultSet.close();
objStatement.close();
objConnection.close();

}
catch (SQLException objSQLException)
{
objSQLException.printStackTrace();
}
}// end of main
}// end of UCPHelloWorld

No comments: