Package org.apache.commons.dbcp.managed
Class ManagedConnection
- java.lang.Object
-
- org.apache.commons.dbcp.AbandonedTrace
-
- org.apache.commons.dbcp.DelegatingConnection
-
- org.apache.commons.dbcp.managed.ManagedConnection
-
- All Implemented Interfaces:
java.lang.AutoCloseable
,java.sql.Connection
,java.sql.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$
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
ManagedConnection.CompletionListener
Delegates totransactionComplete()
for transaction completion events.
-
Field Summary
Fields Modifier and Type Field Description private boolean
accessToUnderlyingConnectionAllowed
private boolean
isSharedConnection
private org.apache.commons.pool.ObjectPool
pool
private TransactionContext
transactionContext
private TransactionRegistry
transactionRegistry
-
Fields inherited from class org.apache.commons.dbcp.DelegatingConnection
_closed, _conn
-
-
Constructor Summary
Constructors Constructor Description ManagedConnection(org.apache.commons.pool.ObjectPool pool, TransactionRegistry transactionRegistry, boolean accessToUnderlyingConnectionAllowed)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
checkOpen()
void
close()
Closes the underlying connection, and close any Statements that were not explicitly closed.void
commit()
java.sql.Connection
getDelegate()
Returns my underlyingConnection
.java.sql.Connection
getInnermostDelegate()
If my underlyingConnection
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()
private void
updateTransactionStatus()
-
Methods inherited from class org.apache.commons.dbcp.DelegatingConnection
abort, activate, clearWarnings, createArrayOf, createBlob, createClob, createNClob, createSQLXML, createStatement, createStatement, createStatement, createStruct, equals, getAutoCommit, getCatalog, getClientInfo, getClientInfo, getDelegateInternal, getHoldability, getInnermostDelegateInternal, getMetaData, getNetworkTimeout, getSchema, 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, setNetworkTimeout, setSavepoint, setSavepoint, setSchema, setTransactionIsolation, setTypeMap, toString, unwrap
-
Methods inherited from class org.apache.commons.dbcp.AbandonedTrace
addTrace, clearTrace, getConfig, getLastUsed, getTrace, printStackTrace, removeTrace, setLastUsed, setLastUsed, setStackTrace
-
-
-
-
Field Detail
-
pool
private final org.apache.commons.pool.ObjectPool pool
-
transactionRegistry
private final TransactionRegistry transactionRegistry
-
accessToUnderlyingConnectionAllowed
private final boolean accessToUnderlyingConnectionAllowed
-
transactionContext
private TransactionContext transactionContext
-
isSharedConnection
private boolean isSharedConnection
-
-
Constructor Detail
-
ManagedConnection
public ManagedConnection(org.apache.commons.pool.ObjectPool pool, TransactionRegistry transactionRegistry, boolean accessToUnderlyingConnectionAllowed) throws java.sql.SQLException
- Throws:
java.sql.SQLException
-
-
Method Detail
-
checkOpen
protected void checkOpen() throws java.sql.SQLException
- Overrides:
checkOpen
in classDelegatingConnection
- Throws:
java.sql.SQLException
-
updateTransactionStatus
private void updateTransactionStatus() throws java.sql.SQLException
- Throws:
java.sql.SQLException
-
close
public void close() throws java.sql.SQLException
Description copied from class:DelegatingConnection
Closes the underlying connection, and close any Statements that were not explicitly closed.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.sql.Connection
- Overrides:
close
in classDelegatingConnection
- Throws:
java.sql.SQLException
-
transactionComplete
protected void transactionComplete()
-
setAutoCommit
public void setAutoCommit(boolean autoCommit) throws java.sql.SQLException
- Specified by:
setAutoCommit
in interfacejava.sql.Connection
- Overrides:
setAutoCommit
in classDelegatingConnection
- Throws:
java.sql.SQLException
-
commit
public void commit() throws java.sql.SQLException
- Specified by:
commit
in interfacejava.sql.Connection
- Overrides:
commit
in classDelegatingConnection
- Throws:
java.sql.SQLException
-
rollback
public void rollback() throws java.sql.SQLException
- Specified by:
rollback
in interfacejava.sql.Connection
- Overrides:
rollback
in classDelegatingConnection
- Throws:
java.sql.SQLException
-
setReadOnly
public void setReadOnly(boolean readOnly) throws java.sql.SQLException
- Specified by:
setReadOnly
in interfacejava.sql.Connection
- Overrides:
setReadOnly
in classDelegatingConnection
- Throws:
java.sql.SQLException
-
isAccessToUnderlyingConnectionAllowed
public boolean isAccessToUnderlyingConnectionAllowed()
If false, getDelegate() and getInnermostDelegate() will return null.- Returns:
- if false, getDelegate() and getInnermostDelegate() will return null
-
getDelegate
public java.sql.Connection getDelegate()
Description copied from class:DelegatingConnection
Returns my underlyingConnection
.- Overrides:
getDelegate
in classDelegatingConnection
- Returns:
- my underlying
Connection
.
-
getInnermostDelegate
public java.sql.Connection getInnermostDelegate()
Description copied from class:DelegatingConnection
If my underlyingConnection
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 classDelegatingConnection
-
-