org.apache.commons.dbcp.managed
Class ManagedConnection

java.lang.Object
  extended by org.apache.commons.dbcp.AbandonedTrace
      extended by org.apache.commons.dbcp.DelegatingConnection
          extended by org.apache.commons.dbcp.managed.ManagedConnection
All Implemented Interfaces:
Connection, Wrapper

public class ManagedConnection
extends DelegatingConnection

ManagedConnection is responsible for managing a database connection in a transactional environment (typically called "Container Managed"). A managed connection operates like any other connection when no global transaction (a.k.a. XA transaction or JTA Transaction) is in progress. When a global transaction is active a single physical connection to the database is used by all ManagedConnections accessed in the scope of the transaction. Connection sharing means that all data access during a transaction has a consistent view of the database. When the global transaction is committed or rolled back the enlisted connections are committed or rolled back. Typically upon transaction completion, a connection returns to the auto commit setting in effect before being enlisted in the transaction, but some vendors do not properly implement this. When enlisted in a transaction the setAutoCommit(), commit(), rollback(), and setReadOnly() methods throw a SQLException. This is necessary to assure that the transaction completes as a single unit.

Version:
$Revision: 892307 $
Author:
Dain Sundstrom

Nested Class Summary
protected  class ManagedConnection.CompletionListener
          Delegates to transactionComplete() for transaction completion events.
 
Field Summary
 
Fields inherited from class org.apache.commons.dbcp.DelegatingConnection
_closed, _conn
 
Fields inherited from interface java.sql.Connection
TRANSACTION_NONE, TRANSACTION_READ_COMMITTED, TRANSACTION_READ_UNCOMMITTED, TRANSACTION_REPEATABLE_READ, TRANSACTION_SERIALIZABLE
 
Constructor Summary
ManagedConnection(ObjectPool pool, TransactionRegistry transactionRegistry, boolean accessToUnderlyingConnectionAllowed)
           
 
Method Summary
protected  void checkOpen()
           
 void close()
          Closes the underlying connection, and close any Statements that were not explicitly closed.
 void commit()
           
 Connection getDelegate()
          Returns my underlying Connection.
 Connection getInnermostDelegate()
          If my underlying Connection is not a DelegatingConnection, returns it, otherwise recursively invokes this method on my delegate.
 boolean isAccessToUnderlyingConnectionAllowed()
          If false, getDelegate() and getInnermostDelegate() will return null.
 void rollback()
           
 void setAutoCommit(boolean autoCommit)
           
 void setReadOnly(boolean readOnly)
           
protected  void transactionComplete()
           
 
Methods inherited from class org.apache.commons.dbcp.DelegatingConnection
activate, clearWarnings, createArrayOf, createBlob, createClob, createNClob, createSQLXML, createStatement, createStatement, createStatement, createStruct, equals, getAutoCommit, getCatalog, getClientInfo, getClientInfo, getDelegateInternal, getHoldability, getInnermostDelegateInternal, getMetaData, getTransactionIsolation, getTypeMap, getWarnings, handleException, hashCode, innermostDelegateEquals, isClosed, isReadOnly, isValid, isWrapperFor, nativeSQL, passivate, prepareCall, prepareCall, prepareCall, prepareStatement, prepareStatement, prepareStatement, prepareStatement, prepareStatement, prepareStatement, releaseSavepoint, rollback, setCatalog, setClientInfo, setClientInfo, setDelegate, setHoldability, setSavepoint, setSavepoint, setTransactionIsolation, setTypeMap, toString, unwrap
 
Methods inherited from class org.apache.commons.dbcp.AbandonedTrace
addTrace, clearTrace, getConfig, getLastUsed, getTrace, printStackTrace, removeTrace, setLastUsed, setLastUsed, setStackTrace
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ManagedConnection

public ManagedConnection(ObjectPool pool,
                         TransactionRegistry transactionRegistry,
                         boolean accessToUnderlyingConnectionAllowed)
                  throws SQLException
Throws:
SQLException
Method Detail

checkOpen

protected void checkOpen()
                  throws SQLException
Overrides:
checkOpen in class DelegatingConnection
Throws:
SQLException

close

public void close()
           throws SQLException
Description copied from class: DelegatingConnection
Closes the underlying connection, and close any Statements that were not explicitly closed.

Specified by:
close in interface Connection
Overrides:
close in class DelegatingConnection
Throws:
SQLException

transactionComplete

protected void transactionComplete()

setAutoCommit

public void setAutoCommit(boolean autoCommit)
                   throws SQLException
Specified by:
setAutoCommit in interface Connection
Overrides:
setAutoCommit in class DelegatingConnection
Throws:
SQLException

commit

public void commit()
            throws SQLException
Specified by:
commit in interface Connection
Overrides:
commit in class DelegatingConnection
Throws:
SQLException

rollback

public void rollback()
              throws SQLException
Specified by:
rollback in interface Connection
Overrides:
rollback in class DelegatingConnection
Throws:
SQLException

setReadOnly

public void setReadOnly(boolean readOnly)
                 throws SQLException
Specified by:
setReadOnly in interface Connection
Overrides:
setReadOnly in class DelegatingConnection
Throws:
SQLException

isAccessToUnderlyingConnectionAllowed

public boolean isAccessToUnderlyingConnectionAllowed()
If false, getDelegate() and getInnermostDelegate() will return null.

Returns:
if false, getDelegate() and getInnermostDelegate() will return null

getDelegate

public Connection getDelegate()
Description copied from class: DelegatingConnection
Returns my underlying Connection.

Overrides:
getDelegate in class DelegatingConnection
Returns:
my underlying Connection.

getInnermostDelegate

public Connection getInnermostDelegate()
Description copied from class: DelegatingConnection
If my underlying Connection is not a DelegatingConnection, returns it, otherwise recursively invokes this method on my delegate.

Hence this method will return the first delegate that is not a DelegatingConnection, or null when no non-DelegatingConnection delegate can be found by traversing this chain.

This method is useful when you may have nested DelegatingConnections, and you want to make sure to obtain a "genuine" Connection.

Overrides:
getInnermostDelegate in class DelegatingConnection


Copyright © 2001-2010 The Apache Software Foundation. All Rights Reserved.