Package io.micronaut.transaction.jdbc
Class ConnectionHolder
- java.lang.Object
-
- io.micronaut.transaction.support.ResourceHolderSupport
-
- io.micronaut.transaction.jdbc.ConnectionHolder
-
- All Implemented Interfaces:
ResourceHolder
public class ConnectionHolder extends ResourceHolderSupport
Resource holder wrapping a JDBCConnection.DataSourceTransactionManagerbinds instances of this class to the thread, for a specificDataSource.Inherits rollback-only support for nested JDBC transactions and reference count functionality from the base class.
Note: This is an SPI class, not intended to be used by applications.
- Since:
- 06.05.2003
- Author:
- Juergen Hoeller
- See Also:
DataSourceTransactionManager,DataSourceUtils
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringSAVEPOINT_NAME_PREFIXPrefix for savepoint names.
-
Constructor Summary
Constructors Constructor Description ConnectionHolder(ConnectionHandle connectionHandle)Create a new ConnectionHolder for the given ConnectionHandle.ConnectionHolder(java.sql.Connection connection)Create a new ConnectionHolder for the given JDBC Connection, wrapping it with aSimpleConnectionHandle, assuming that there is no ongoing transaction.ConnectionHolder(java.sql.Connection connection, boolean transactionActive)Create a new ConnectionHolder for the given JDBC Connection, wrapping it with aSimpleConnectionHandle.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Clear the transactional state of this resource holder.java.sql.SavepointcreateSavepoint()Create a new JDBC 3.0 Savepoint for the current Connection, using generated savepoint names that are unique for the Connection.java.sql.ConnectiongetConnection()Return the current Connection held by this ConnectionHolder.ConnectionHandlegetConnectionHandle()protected booleanhasConnection()protected booleanisTransactionActive()voidreleased()Releases the current Connection held by this ConnectionHolder.protected voidsetConnection(java.sql.Connection connection)Override the existing Connection handle with the given Connection.protected voidsetTransactionActive(boolean transactionActive)Set whether this holder represents an active, JDBC-managed transaction.booleansupportsSavepoints()Return whether JDBC 3.0 Savepoints are supported.-
Methods inherited from class io.micronaut.transaction.support.ResourceHolderSupport
getDeadline, getTimeToLiveInMillis, getTimeToLiveInSeconds, hasTimeout, isOpen, isRollbackOnly, isSynchronizedWithTransaction, isVoid, requested, reset, resetRollbackOnly, setRollbackOnly, setSynchronizedWithTransaction, setTimeout, setTimeoutInMillis, unbound
-
-
-
-
Field Detail
-
SAVEPOINT_NAME_PREFIX
public static final java.lang.String SAVEPOINT_NAME_PREFIX
Prefix for savepoint names.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ConnectionHolder
public ConnectionHolder(ConnectionHandle connectionHandle)
Create a new ConnectionHolder for the given ConnectionHandle.- Parameters:
connectionHandle- the ConnectionHandle to hold
-
ConnectionHolder
public ConnectionHolder(java.sql.Connection connection)
Create a new ConnectionHolder for the given JDBC Connection, wrapping it with aSimpleConnectionHandle, assuming that there is no ongoing transaction.- Parameters:
connection- the JDBC Connection to hold- See Also:
SimpleConnectionHandle,ConnectionHolder(java.sql.Connection, boolean)
-
ConnectionHolder
public ConnectionHolder(java.sql.Connection connection, boolean transactionActive)Create a new ConnectionHolder for the given JDBC Connection, wrapping it with aSimpleConnectionHandle.- Parameters:
connection- the JDBC Connection to holdtransactionActive- whether the given Connection is involved in an ongoing transaction- See Also:
SimpleConnectionHandle
-
-
Method Detail
-
getConnectionHandle
@Nullable public ConnectionHandle getConnectionHandle()
- Returns:
- Return the ConnectionHandle held by this ConnectionHolder.
-
hasConnection
protected boolean hasConnection()
- Returns:
- Return whether this holder currently has a Connection.
-
setTransactionActive
protected void setTransactionActive(boolean transactionActive)
Set whether this holder represents an active, JDBC-managed transaction.- Parameters:
transactionActive- Whether a transaction is active- See Also:
DataSourceTransactionManager
-
isTransactionActive
protected boolean isTransactionActive()
- Returns:
- Return whether this holder represents an active, JDBC-managed transaction.
-
setConnection
protected void setConnection(@Nullable java.sql.Connection connection)Override the existing Connection handle with the given Connection. Reset the handle if givennull.Used for releasing the Connection on suspend (with a
nullargument) and setting a fresh Connection on resume.- Parameters:
connection- The connection
-
getConnection
public java.sql.Connection getConnection()
Return the current Connection held by this ConnectionHolder.This will be the same Connection until
releasedgets called on the ConnectionHolder, which will reset the held Connection, fetching a new Connection on demand.- Returns:
- The connection
- See Also:
ConnectionHandle.getConnection(),released()
-
supportsSavepoints
public boolean supportsSavepoints() throws java.sql.SQLExceptionReturn whether JDBC 3.0 Savepoints are supported. Caches the flag for the lifetime of this ConnectionHolder.- Returns:
- Whether savepoints are supported
- Throws:
java.sql.SQLException- if thrown by the JDBC driver
-
createSavepoint
public java.sql.Savepoint createSavepoint() throws java.sql.SQLExceptionCreate a new JDBC 3.0 Savepoint for the current Connection, using generated savepoint names that are unique for the Connection.- Returns:
- the new Savepoint
- Throws:
java.sql.SQLException- if thrown by the JDBC driver
-
released
public void released()
Releases the current Connection held by this ConnectionHolder.This is necessary for ConnectionHandles that expect "Connection borrowing", where each returned Connection is only temporarily leased and needs to be returned once the data operation is done, to make the Connection available for other operations within the same transaction.
- Overrides:
releasedin classResourceHolderSupport
-
clear
public void clear()
Description copied from class:ResourceHolderSupportClear the transactional state of this resource holder.- Overrides:
clearin classResourceHolderSupport
-
-