OC4J connection caching problem-Help pls

Hi Friends

My problem is explained below:

--

JDBC Connection Caching using OC4J

I have Created a Data Source under a specific OC4J(oc4j_alc1) Instance using 9iAS Enterprise Manager Web Site using the following values:

GENERAL SECTION

Name: OracleAlcDS

Description: Datasource For ALC Logistics

Data Source Class: oracle.jdbc.pool.OracleConnectionCacheImpl

Schema:

Username: alcw

Password: alcw

JDBC_URL: jdbc:oracle:thin:@206.101.32.33:1521:store

JDBC Driver: oracle.jdbc.driver.OracleDriver

JNDI LOCATIONS SECTION

Location: jdbc/OracleAlcDS

Transactional (XA) Location:

EJB Location:

CONNECTION ATTRIBUTES SECTION

Connection Retry Interval (secs): 1

Max Connection Attempts: 5

Cached Connection Inactivity Timeout(secs): 60

Maximum Open Connections: 20

Minimum Open Connections: 5

Wait for Free Connection Timeout (secs): 60

The connection cache will be instantiated as soon as the OC4J instance

is started. In the case of the example above, five connections will be

instantiated immediately up to a maximum of 30.

PROPERTY SECTION

cache_scheme :FIXED_RETURN_NULL_SCHEME

(So that the maximum limit should not be exceeded)

Step 2.

Created Java classes to retrieve and use a cached connection.

This class is used as factory to return pooled/cached connections to the requestor.

package com.allenLund.dbConnection;

import java.sql.*;

import javax.sql.*;

import javax.naming.*;

public class MyDBConnectionPool

{

private static InitialContext context = null;

private static DataSource jdbcURL = null;

private static InitialContext contextAdmin = null;

private static DataSource jdbcURLAdmin = null;

private static void initJDBCConnectionFactory()

{

try {

context = new InitialContext();

jdbcURL = (DataSource) context.lookup("jdbc/OracleAlcDS");

}catch(NamingException e)

{

System.err.println("Error looking up Data Source from Factory: "+e.getMessage());

}

}

public static Connection getPooledConnection()

{

try

{

// Lazy initialization

if (jdbcURL == null)

{

initJDBCConnectionFactory();

System.out.println("Initialization Successfull");

}

//Returns an available connection from the connection cache.

return jdbcURL.getConnection();

}catch(SQLException e)

{

System.out.println("Error getting pooled connection from Factory:"+e.getMessage());

return null;

}

}

//for Admin(for current application just ignore this method

private static void initJDBCConnectionFactoryAdmin()

{

try {

contextAdmin = new InitialContext();

jdbcURLAdmin = (DataSource) context.lookup("jdbc/OracleAlcAdminDS");

System.out.println("Obtained Cached Data Source ");

}catch(NamingException e)

{

System.err.println("Error looking up Data Source from Factory: "+e.getMessage());

}

}

//for Admin(for current application just ignore this method

public static Connection getPooledConnectionAdmin()

{

try

{

// Lazy initialization

if (jdbcURLAdmin == null)

{

initJDBCConnectionFactoryAdmin();

System.out.println("the second connection************************");

}

//Returns an available connection from the connection cache.

return jdbcURLAdmin.getConnection();

}catch(SQLException e)

{

System.out.println("Error getting pooled connection from Factory:"+e.getMessage());

return null;

}

}

}

Next step is how I am using this Base class in my application

We are creating an instance of MyDBConnectionPool class in our LoginServlet

(LoginServlet.java)and setting in our application context.

Code snippet:

ServletContext context = config.getServletContext();

connectionPool = new MyDBConnectionPool();

context.setAttribute("connectionPool", connectionPool);

In the jsp pages where database connection is required we are doing

the following steps :

<%!

private Connection conn = null;

private MyDBConnectionPool connectionPool = null;

%>

<%

// DB connection pool

if (connectionPool == null)

connectionPool = (MyDBConnectionPool)application.getAttribute("connectionPool");

conn = connectionPool.getPooledConnection();

.

.

.

conn.close();

%>

I have used various combination of these attributes

Cached Connection Inactivity Timeout(secs):

Maximum Open Connections:

Minimum Open Connections:

But it did not work.

But I think caching of connection is not happening properly.

As a result, I feel instead of using connection from the pool everytime a new connection is being created.

growing number of connections() are pointing towards this.

Open JDBC Connections are exceeding the value Maximum Open Connections

even if we are using cache_scheme :FIXED_RETURN_NULL_SCHEME

Please guide me to find the problem.

[5321 byte] By [mehbub_shaik@yahoo.coma] at [2007-10-1 5:24:42]
# 1

>>We are creating an instance of MyDBConnectionPool class in our LoginServlet

>>(LoginServlet.java)and setting in our application context.

>>Code snippet:

>>ServletContext context = config.getServletContext();

>>connectionPool = new MyDBConnectionPool();

>>context.setAttribute("connectionPool", connectionPool);

I am a Java newbie (Oracle DBA by trade), so if my answer is completely off base, I hereby apologize in advance...

However, I too am trying to develop a connection pool implementation for Oracle's proxy/OCI authentication mechanism (as getProxyConnection() method appears unsupported under OC4J/OAS/JNDI interfaces to datasources/connection pooling).

Anyway, isn't your problem that your login servlet is creating a "new" pool each time it's invoked? In other words, should not your MyDBConnectionPool class be a "singleton" class not supporting "new" (class having private constructor) and guaranteeing one-time, synchronized initialization of your static pool/connection url upon the first (and only the first) request to MyDBConnectionPool.getPooledConnection() method? It seems to me that by instantiating your class repeatedly in your login servlet, despite your private static url in your pool class, that you may be creating MANY pools when you really want just one (singleton) pool. Anyway, I just don't know enough about oc4j connection pooling or servlets to know for sure. But it's a thought. My own pool class, for instance was coded as a singleton where its clients never "new" the class. They just call its getProxyConnection() method.

Good luck.

Todd

ToddRydera at 2007-7-9 13:19:12 > top of Java-index,Administration Tools,Sun Connection...
# 2

It looks like you got this code from my article (http://www.oracle.com/technology/oramag/code/tips2003/092103.html). The article defined two classes. The first class ,JDBCConnectionFactory, was designed to be a Factory class or Singleton. The second class, JDBCConnectionBase, was designed to be extended (for the sake of convenience). The second class leverages the factory class. I've been using this for a couple of years. Anyway, if you want to grab a connection from the factory class, just make a call such as conn = JDBCConnectionFactory.getPooledConnection(). Since it is a static class, it will be available to all other classes in your application. There is no need to create an instance of it in your servlet. If you have questions or comments you get to me via http://radio.weblogs.com/0137094/2004/11/16.html. Good luck with your app!

jasbena at 2007-7-9 13:19:12 > top of Java-index,Administration Tools,Sun Connection...