Package org.apache.commons.dbcp2.managed
Class ManagedConnection<C extends Connection>
java.lang.Object
org.apache.commons.dbcp2.AbandonedTrace
org.apache.commons.dbcp2.DelegatingConnection<C>
org.apache.commons.dbcp2.managed.ManagedConnection<C>
- Type Parameters:
C
- the Connection type
- All Implemented Interfaces:
AutoCloseable
,Connection
,Wrapper
,org.apache.commons.pool2.TrackedUse
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.
- Since:
- 2.0
- Version:
- $Id: ManagedConnection.java 1658616 2015-02-10 02:49:54Z psteitz $
- Author:
- Dain Sundstrom
-
Field Summary
Fields inherited from interface java.sql.Connection
TRANSACTION_NONE, TRANSACTION_READ_COMMITTED, TRANSACTION_READ_UNCOMMITTED, TRANSACTION_REPEATABLE_READ, TRANSACTION_SERIALIZABLE
-
Constructor Summary
ConstructorsConstructorDescriptionManagedConnection
(org.apache.commons.pool2.ObjectPool<C> pool, TransactionRegistry transactionRegistry, boolean accessToUnderlyingConnectionAllowed) -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes the underlying connection, and close any Statements that were not explicitly closed.void
commit()
Returns my underlyingConnection
.If my underlyingConnection
is not aDelegatingConnection
, returns it, otherwise recursively invokes this method on my delegate.boolean
If false, getDelegate() and getInnermostDelegate() will return null.void
rollback()
void
setAutoCommit
(boolean autoCommit) void
setReadOnly
(boolean readOnly) Methods inherited from class org.apache.commons.dbcp2.DelegatingConnection
abort, clearCachedState, clearWarnings, createArrayOf, createBlob, createClob, createNClob, createSQLXML, createStatement, createStatement, createStatement, createStruct, getAutoCommit, getCacheState, getCatalog, getClientInfo, getClientInfo, getDefaultQueryTimeout, getHoldability, getInnermostDelegateInternal, getMetaData, getNetworkTimeout, getSchema, getTransactionIsolation, getTypeMap, getWarnings, innermostDelegateEquals, isClosed, isReadOnly, isValid, isWrapperFor, nativeSQL, prepareCall, prepareCall, prepareCall, prepareStatement, prepareStatement, prepareStatement, prepareStatement, prepareStatement, prepareStatement, releaseSavepoint, rollback, setCacheState, setCatalog, setClientInfo, setClientInfo, setDefaultQueryTimeout, setDelegate, setHoldability, setNetworkTimeout, setSavepoint, setSavepoint, setSchema, setTransactionIsolation, setTypeMap, toString, unwrap
Methods inherited from class org.apache.commons.dbcp2.AbandonedTrace
getLastUsed
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.sql.Connection
beginRequest, endRequest, setShardingKey, setShardingKey, setShardingKeyIfValid, setShardingKeyIfValid
-
Constructor Details
-
ManagedConnection
public ManagedConnection(org.apache.commons.pool2.ObjectPool<C> pool, TransactionRegistry transactionRegistry, boolean accessToUnderlyingConnectionAllowed) throws SQLException - Throws:
SQLException
-
-
Method Details
-
close
Description copied from class:DelegatingConnection
Closes the underlying connection, and close any Statements that were not explicitly closed. Sub-classes that override this method must:- Call passivate()
- Call close (or the equivalent appropriate action) on the wrapped connection
- Set _closed to
false
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceConnection
- Overrides:
close
in classDelegatingConnection<C extends Connection>
- Throws:
SQLException
-
setAutoCommit
- Specified by:
setAutoCommit
in interfaceConnection
- Overrides:
setAutoCommit
in classDelegatingConnection<C extends Connection>
- Throws:
SQLException
-
commit
- Specified by:
commit
in interfaceConnection
- Overrides:
commit
in classDelegatingConnection<C extends Connection>
- Throws:
SQLException
-
rollback
- Specified by:
rollback
in interfaceConnection
- Overrides:
rollback
in classDelegatingConnection<C extends Connection>
- Throws:
SQLException
-
setReadOnly
- Specified by:
setReadOnly
in interfaceConnection
- Overrides:
setReadOnly
in classDelegatingConnection<C extends Connection>
- Throws:
SQLException
-
isAccessToUnderlyingConnectionAllowed
public boolean isAccessToUnderlyingConnectionAllowed()If false, getDelegate() and getInnermostDelegate() will return null.- Returns:
- if false, getDelegate() and getInnermostDelegate() will return null
-
getDelegate
Description copied from class:DelegatingConnection
Returns my underlyingConnection
.- Overrides:
getDelegate
in classDelegatingConnection<C extends Connection>
- Returns:
- my underlying
Connection
.
-
getInnermostDelegate
Description copied from class:DelegatingConnection
If my underlyingConnection
is not aDelegatingConnection
, returns it, otherwise recursively invokes this method on my delegate.Hence this method will return the first delegate that is not a
DelegatingConnection
, ornull
when no non-DelegatingConnection
delegate can be found by traversing this chain.This method is useful when you may have nested
DelegatingConnection
s, and you want to make sure to obtain a "genuine"Connection
.- Overrides:
getInnermostDelegate
in classDelegatingConnection<C extends Connection>
-