Andrej Koelewijn

10/21/2003

A PooledConnection is not a pool of connections

Filed under: — andrejk @ 3:20 pm

I ran into this problem again today as the naming of these objects is quite confusing.



It’s a quite common mistake to use an OracleConnectionPoolDataSource when you need a pool of database connections. However, OracleConnectionPoolDataSource represents one poolable physical database connection. Usually you’ll want to use an OracleConnectionCacheImpl. This object will provide a pool of reusable connection. You can specify the minimum and maximum number of physical database connection you want, and what you want to do when all physical database connections are in use.



The following example shows how you can use the OracleConnectionPoolDataSource.



/*

* Created on Oct 21, 2003 */
package org.ako.testoraclepool;

import oracle.jdbc.pool.*;
import java.sql.*;

/**

* @author akoelewi */
public class Main {

/*

* Thread to run query */
class QueryThread extends Thread {
Connection connection;
int id;
public QueryThread (String name , Connection _connection , int _id) {
super(name);
connection = _connection;
id = _id;
}
public void run() {
try {
queryUser(this.connection, this.id);
} catch (Exception e) {
e.printStackTrace();
}
}
private void queryUser(Connection _connection , int _id)
throws SQLException {

System.out.println("Starting query: " + _id);
PreparedStatement stmt =
_connection.prepareStatement(
"select * from v$session"

+ " where machine like 'akoelewi%' " + " and program like 'JDBC%' " + " order by program ");
ResultSet rset = stmt.executeQuery();
while (rset.next()) {
System.out.println(
"Query #"
+ _id
+ ", osuser: "
+ rset.getString("OSUSER")
+ ", machine: "
+ rset.getString("MACHINE")
+ ", program: "
+ rset.getString("PROGRAM")
+ ", sid: "
+ rset.getString("SID")
+ ", serial: "
+ rset.getString("SERIAL#"));
}

_connection.close();
System.out.println("Done query: " + _id);
}
}

public Main() throws Exception {

OracleConnectionCacheImpl ods =

new OracleConnectionCacheImpl();
ods.setURL("jdbc:oracle:thin:@machine:port:sid");
ods.setUser("username");
ods.setPassword("password");
ods.setMaxLimit(4);
ods.setMinLimit(2);
ods.setCacheScheme (OracleConnectionCacheImpl.FIXED_WAIT_SCHEME);

for (int i = 0; i < 10; i++) {
QueryThread queryThread =
new QueryThread("query" + i

, ods.getConnection(), i);
queryThread.start();
}

}

public static void main(String[] args) throws Exception {
Main main = new Main();
}
}

5 Responses to “A PooledConnection is not a pool of connections”

  1. No one Says:

    When you do that, your code is tied directly to Oracle. Are you sure you really want to do that? Not very smart most of the time…

  2. Andrej Says:

    As far as i know this has not been standardised yet in jdbc. If you look at the jdbc 3.0 specs, you’ll see that their example also use some com.acme… connection pool driver. So if you are going to manually instantiate a connection pool you’ll have to use non standard code.
    Ofcourse, you should put the instantiation in a factory, hiding it from the rest of your application. The oracle connection cache is a datasource, so you register it with jndi, and use it as a datasource in the rest of your application

  3. F. Degenaar Says:

    Since OracleConnectionCacheImpl does implement javax.sql.DataSource, one is not tied directly to Oracle.
    On the other hand there is no such connection pool for XA-connections, which is far more annoying.

    Fokko

  4. bango Says:

    if im not mistaken, PooledConnection actually a physical connection changed logical connection that we call connection pooling, the problem is that it only reserved one physical connection, so when the demands are high for logical connections, we have to wait quite long time…
    and that’s why we use connection caching that would provide necessary physical connections when needed and only give free logical connection (depends on cache scheme)

  5. Anonymous Says:

    me kip

Leave a Reply

Powered by WordPress