| Java Doc By Examples | |
| Prev Class | Next Class | Frames | No Frames |
| Summary: Nested | Field | Method | Constr | Detail: Nested | Field | Method | Constr |
public interface PooledConnectionPooledConnection 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.
Method Summary | |
void |
|
void |
|
Connection |
|
void |
|
public void addConnectionEventListener(ConnectionEventListener listener)
Registers the given event listener so that it will be notified when an event occurs on thisPooledConnectionobject.
- Parameters:
listener- a component, usually the connection pool manager, that has implemented theConnectionEventListenerinterface and wants to be notified when the connection is closed or has an error
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);
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);
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
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().");
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) {
public void close() throws SQLException
Closes the physical connection that thisPooledConnectionobject represents. An application never calls this method directly; it is called by the connection pool module, or manager. See theinterface descriptionfor more information.
- Throws:
SQLException- if a database access error occurs
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();
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();
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();
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();
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;
public Connection getConnection() throws SQLException
Creates and returns aConnectionobject that is a handle for the physical connection that thisPooledConnectionobject represents. The connection pool manager calls this method when an application has called the methodDataSource.getConnectionand there are noPooledConnectionobjects available. See theinterface descriptionfor more information.
- Returns:
- a
Connectionobject that is a handle to thisPooledConnectionobject
- Throws:
SQLException- if a database access error occurs
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());
1: = (ConnectionPoolDataSource) initCtx.lookup( jndiName ); 2: PooledConnection pooledConnection = dataSource.getPooledConnection(); 3: } 4: 5: private static InitialContext createContext() throws NamingException {
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();
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();
public void removeConnectionEventListener(ConnectionEventListener listener)
Removes the given event listener from the list of components that will be notified when an event occurs on thisPooledConnectionobject.
- Parameters:
listener- a component, usually the connection pool manager, that has implemented theConnectionEventListenerinterface and been registered with thisPooledConnectionobject as a 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);
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);
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);
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);
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);