javax.sql

Interface PooledConnection

Known Subinterfaces:
XAConnection

public interface PooledConnection

An object that provides hooks for connection pool management. A PooledConnection object represents a physical connection to a data source. The connection can be recycled rather than being closed when an application is finished with it, thus reducing the number of connections that need to be made.

An application programmer does not use the PooledConnection interface directly; rather, it is used by a middle tier infrastructure that manages the pooling of connections.

When an application calls the method DataSource.getConnection, it gets back a Connection object. If connection pooling is being done, that Connection object is actually a handle to a PooledConnection object, which is a physical connection.

The connection pool manager, typically the application server, maintains a pool of PooledConnection objects. If there is a PooledConnection object available in the pool, the connection pool manager returns a Connection object that is a handle to that physical connection. If no PooledConnection object is available, the connection pool manager calls the PooledConnection method getConnection to create a new physical connection and returns a handle to it.

When an application closes a connection, it calls the Connection method close. When connection pooling is being done, the connection pool manager is notified because it has registered itself as a ConnectionEventListener object using the ConnectionPool method addConnectionEventListener. The connection pool manager deactivates the handle to the PooledConnection object and returns the PooledConnection object to the pool of connections so that it can be used again. Thus, when an application closes its connection, the underlying physical connection is recycled rather than being closed.

The physical connection is not closed until the connection pool manager calls the PooledConnection method close. This method is generally called to have an orderly shutdown of the server or if a fatal error has made the connection unusable.

Since:
1.4

Method Summary

void
addConnectionEventListener(ConnectionEventListener listener)
Registers the given event listener so that it will be notified when an event occurs on this PooledConnection object.
void
close()
Closes the physical connection that this PooledConnection object represents.
Connection
getConnection()
Creates and returns a Connection object that is a handle for the physical connection that this PooledConnection object represents.
void
removeConnectionEventListener(ConnectionEventListener listener)
Removes the given event listener from the list of components that will be notified when an event occurs on this PooledConnection object.

Method Details

addConnectionEventListener

public void addConnectionEventListener(ConnectionEventListener listener)
Registers the given event listener so that it will be notified when an event occurs on this PooledConnection object.
Parameters:
listener - a component, usually the connection pool manager, that has implemented the ConnectionEventListener interface and wants to be notified when the connection is closed or has an error
Usages and Demos :

View More Examples of addConnectionEventListener(ConnectionEventListener listener)
   1: import javax.sql.ConnectionEventListener;
   2: import javax.sql.PooledConnection;
   3: import java.sql.Connection;
   4:         ...
   5: public final class PooledConnectionWrapper
   6:   implements PooledConnection
   7: {
   8:         ...
   9:   private final PooledConnection _connection;
  10:   private final ProfilerPoint _profilerPoint;
  11:         ...
  12:   {
  13:     _connection.addConnectionEventListener(listener);

View Full Code Here
   1: import javax.sql.ConnectionEventListener;
   2: import javax.sql.PooledConnection;
   3: import java.sql.Connection;
   4:         ...
   5: 
   6: public class SpyPooledConnection implements javax.sql.PooledConnection {
   7:   protected final static Logger log = Log.open(SpyPooledConnection.class);
   8:         ...
   9: 
  10:   private PooledConnection _pconn;
  11: 
  12:         ...
  13:   {
  14:     _pconn.addConnectionEventListener(listener);

View Full Code Here
   1:             { 
   2:                 PooledConnection out = (auth.equals( C3P0Utils.NULL_AUTH ) ?
   3:                             cpds.getPooledConnection() :
   4:         ...
   5:                 ((C3P0PooledConnection) out).initStatementCache(scmgr);
   6:                 out.addConnectionEventListener( cl );
   7:                 return out;
   8:         ...
   9:             public void destroyResource(Object resc) throws Exception
  10:             { ((PooledConnection) resc).close();}
  11:             };
  12:         ...
  13:     
  14:     public PooledConnection checkoutPooledConnection() throws SQLException

View Full Code Here
   1: import javax.sql.ConnectionEvent;
   2: import javax.sql.PooledConnection;
   3: 
   4:         ...
   5:         final ConnectionEventListener conListener = new ConnectionListener();
   6:         PooledConnection pc = null;
   7:         final int NB_TESTS = 5;
   8:         ...
   9:             
  10:             pc.addConnectionEventListener(conListener);
  11:             
  12:         ...
  13:                 try {
  14:                     System.out.println("Before pooledConnection.close().");

View Full Code Here
   1: import javax.sql.ConnectionPoolDataSource;
   2: import javax.sql.PooledConnection;
   3: import javax.sql.ConnectionEvent;
   4:         ...
   5:     synchronized public Object makeObject() {
   6:         PooledConnection pc = null;
   7:         try
   8:         ...
   9:             }
  10:             pc.addConnectionEventListener(this);
  11:         }
  12:         ...
  13:     public void destroyObject(Object obj) {
  14:         if(obj instanceof PooledConnection) {

View Full Code Here

close

public void close()
            throws SQLException
Closes the physical connection that this PooledConnection object represents. An application never calls this method directly; it is called by the connection pool module, or manager.

See the interface description for more information.

Throws:
SQLException - if a database access error occurs
Usages and Demos :

View More Examples of close()
   1: import java.sql.Connection;
   2: import javax.sql.PooledConnection;
   3: import oracle.jdbc.pool.OracleConnectionPoolDataSource;
   4:         ...
   5:     ocpds.setURL ( "jdbc:oracle:thin:@usunrat24.us.oracle.com:1521:ora92i" ); 
   6:     PooledConnection pooledConnection = ocpds.getPooledConnection();
   7:     InputUtil.waitTillUserHitsEnter("Done creating pooled connection.");
   8:         ...
   9:     InputUtil.waitTillUserHitsEnter("Done getting connection from pooled connection object.");
  10:     connection.close();
  11:     InputUtil.waitTillUserHitsEnter("Done closing logical connection");
  12:         ...
  13:     pooledConnection.close();

View Full Code Here
   1: import javax.sql.ConnectionEventListener;
   2: import javax.sql.PooledConnection;
   3: import java.sql.Connection;
   4:         ...
   5: public final class PooledConnectionWrapper
   6:   implements PooledConnection
   7: {
   8:         ...
   9:   private final PooledConnection _connection;
  10:   private final ProfilerPoint _profilerPoint;
  11:         ...
  12:   {
  13:     _connection.close();

View Full Code Here
   1: import javax.sql.ConnectionEventListener;
   2: import javax.sql.PooledConnection;
   3: import java.sql.Connection;
   4:         ...
   5: 
   6: public class SpyPooledConnection implements javax.sql.PooledConnection {
   7:   protected final static Logger log = Log.open(SpyPooledConnection.class);
   8:         ...
   9: 
  10:   private PooledConnection _pconn;
  11: 
  12:         ...
  13:     try {
  14:       _pconn.close();

View Full Code Here
   1: import javax.sql.ConnectionEvent;
   2: import javax.sql.PooledConnection;
   3: 
   4:         ...
   5:         final ConnectionEventListener conListener = new ConnectionListener();
   6:         PooledConnection pc = null;
   7:         final int NB_TESTS = 5;
   8:         ...
   9:                 
  10:                 System.out.println("Before connection.close().");
  11:                 
  12:         ...
  13:                 conn.close();

View Full Code Here
   1: import javax.sql.ConnectionPoolDataSource;
   2: import javax.sql.PooledConnection;
   3: 
   4:         ...
   5:             = "close() was called on a Connection, but "
   6:             + "I have no record of the underlying PooledConnection.";
   7: 
   8:         ...
   9:             try {
  10:                 _pool.close();
  11:             } catch (RuntimeException e) {
  12:         ...
  13: 
  14:         PooledConnection pc = null;

View Full Code Here

getConnection

public Connection getConnection()
            throws SQLException
Creates and returns a Connection object that is a handle for the physical connection that this PooledConnection object represents. The connection pool manager calls this method when an application has called the method DataSource.getConnection and there are no PooledConnection objects available. See the interface description for more information.
Returns:
a Connection object that is a handle to this PooledConnection object
Throws:
SQLException - if a database access error occurs
Usages and Demos :

View More Examples of getConnection()
   1: import javax.sql.ConnectionEventListener;
   2: import javax.sql.PooledConnection;
   3: import java.sql.Connection;
   4:         ...
   5: public final class PooledConnectionWrapper
   6:   implements PooledConnection
   7: {
   8:         ...
   9:   private final PooledConnection _connection;
  10:   private final ProfilerPoint _profilerPoint;
  11:         ...
  12:   {
  13:     return wrap(_connection.getConnection());

View Full Code Here
   1:         = (ConnectionPoolDataSource) initCtx.lookup( jndiName );
   2:         PooledConnection pooledConnection = dataSource.getPooledConnection();
   3:     }
   4:     
   5:     private static InitialContext createContext() throws NamingException {

View Full Code Here
   1: import javax.sql.ConnectionPoolDataSource;
   2: import javax.sql.PooledConnection;
   3: import com.mchange.v2.sql.SqlUtils;
   4:         ...
   5:     {    
   6:     PooledConnection pc = poolManager.getPool().checkoutPooledConnection();
   7:         ...
   8:     return pc.getConnection();
   9:     }
  10:         ...
  11:     { 
  12:     PooledConnection pc = poolManager.getPool(new DbAuth(username, password)).checkoutPooledConnection();

View Full Code Here
   1: import javax.sql.DataSource;
   2: import javax.sql.PooledConnection;
   3: import javax.sql.ConnectionPoolDataSource;
   4:         ...
   5:         ConnectionPoolDataSource ds = (ConnectionPoolDataSource) datasource;
   6:         PooledConnection pc = ds.getPooledConnection();
   7:         ...
   8:         return pc.getConnection();
   9:     }
  10:         ...
  11:         DataSource ds = (DataSource) datasource;
  12:         return ds.getConnection();

View Full Code Here
   1:     {
   2:     PooledConnection pc = getPoolManager().getPool().checkoutPooledConnection();
   3:         ...
   4:     return pc.getConnection();
   5:     }
   6:         ...
   7:     { 
   8:     PooledConnection pc = getPoolManager().getPool(username, password).checkoutPooledConnection();
   9:         ...
  10:     return pc.getConnection();

View Full Code Here

removeConnectionEventListener

public void removeConnectionEventListener(ConnectionEventListener listener)
Removes the given event listener from the list of components that will be notified when an event occurs on this PooledConnection object.
Parameters:
listener - a component, usually the connection pool manager, that has implemented the ConnectionEventListener interface and been registered with this PooledConnection object as a listener
Usages and Demos :

View More Examples of removeConnectionEventListener(ConnectionEventListener listener)
   1: import javax.sql.ConnectionEventListener;
   2: import javax.sql.PooledConnection;
   3: import java.sql.Connection;
   4:         ...
   5: public final class PooledConnectionWrapper
   6:   implements PooledConnection
   7: {
   8:         ...
   9:   private final PooledConnection _connection;
  10:   private final ProfilerPoint _profilerPoint;
  11:         ...
  12:   {
  13:     _connection.removeConnectionEventListener(listener);

View Full Code Here
   1: import javax.sql.ConnectionEventListener;
   2: import javax.sql.PooledConnection;
   3: import java.sql.Connection;
   4:         ...
   5: 
   6: public class SpyPooledConnection implements javax.sql.PooledConnection {
   7:   protected final static Logger log = Log.open(SpyPooledConnection.class);
   8:         ...
   9: 
  10:   private PooledConnection _pconn;
  11: 
  12:         ...
  13:   {
  14:     _pconn.removeConnectionEventListener(listener);

View Full Code Here
   1: import javax.sql.ConnectionPoolDataSource;
   2: import javax.sql.PooledConnection;
   3: import javax.sql.ConnectionEvent;
   4:         ...
   5:     synchronized public Object makeObject() {
   6:         PooledConnection pc = null;
   7:         try
   8:         ...
   9:     public void destroyObject(Object obj) {
  10:         if(obj instanceof PooledConnection) {
  11:             try {
  12:         ...
  13:                 .println("CLOSING DOWN CONNECTION DUE TO INTERNAL ERROR");
  14:             pc.removeConnectionEventListener(this);

View Full Code Here
   1: import javax.sql.ConnectionPoolDataSource;
   2: import javax.sql.PooledConnection;
   3: import javax.sql.ConnectionEvent;
   4:         ...
   5:         String username = upkey.getUsername();
   6:         PooledConnection pc = null;
   7:         try
   8:         ...
   9:     public void destroyObject(Object key, Object obj) {
  10:         if(obj instanceof PooledConnection) {
  11:             try {
  12:         ...
  13:                 .println("CLOSING DOWN CONNECTION DUE TO INTERNAL ERROR");
  14:             pc.removeConnectionEventListener(this);

View Full Code Here
   1: import javax.sql.ConnectionPoolDataSource;
   2: import javax.sql.PooledConnection;
   3: 
   4:         ...
   5:             = "close() was called on a Connection, but "
   6:             + "I have no record of the underlying PooledConnection.";
   7: 
   8:         ...
   9:         try {
  10:             PooledConnection pc = null;
  11:             if (_username == null) {
  12:         ...
  13:             }
  14:             pc.removeConnectionEventListener(this);

View Full Code Here