Top Description Fields Constructors Methods
org.springframework.jdbc.datasource

public Class ConnectionHolder

extends ResourceHolderSupport
Class Inheritance
Imports
java.sql.Connection, .SQLException, .Savepoint, org.springframework.lang.Nullable, org.springframework.transaction.support.ResourceHolderSupport, org.springframework.util.Assert

Resource holder wrapping a JDBC Connection. DataSourceTransactionManager binds instances of this class to the thread, for a specific javax.sql.DataSource.

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.

Author
Juergen Hoeller
Since
06.05.2003
See Also
DataSourceTransactionManager, DataSourceUtils

Field Summary

Modifier and TypeField and Description
private ConnectionHandle
private Connection
public static final String
SAVEPOINT_NAME_PREFIX

Prefix for savepoint names.

private int
private Boolean
private boolean

Constructor Summary

AccessConstructor and Description
public
ConnectionHolder(ConnectionHandle
the ConnectionHandle to hold
connectionHandle
)

Create a new ConnectionHolder for the given ConnectionHandle.

public
ConnectionHolder(Connection
the JDBC Connection to hold
connection
)

Create a new ConnectionHolder for the given JDBC Connection, wrapping it with a SimpleConnectionHandle, assuming that there is no ongoing transaction.

public
ConnectionHolder(Connection
the JDBC Connection to hold
connection
,
boolean
whether the given Connection is involved in an ongoing transaction
transactionActive
)

Create a new ConnectionHolder for the given JDBC Connection, wrapping it with a SimpleConnectionHandle.

Method Summary

Modifier and TypeMethod and Description
public void
clear()

Overrides org.springframework.transaction.support.ResourceHolderSupport.clear.

Clear the transactional state of this resource holder.

public Savepoint

Returns:

the new Savepoint
createSavepoint
()

Create a new JDBC Savepoint for the current Connection, using generated savepoint names that are unique for the Connection.

public Connection
getConnection()

Return the current Connection held by this ConnectionHolder.

public ConnectionHandle
getConnectionHandle()

Return the ConnectionHandle held by this ConnectionHolder.

protected boolean
hasConnection()

Return whether this holder currently has a Connection.

protected boolean
isTransactionActive()

Return whether this holder represents an active, JDBC-managed transaction.

public void
released()

Overrides org.springframework.transaction.support.ResourceHolderSupport.released.

Releases the current Connection held by this ConnectionHolder.

protected void
setConnection(Connection connection)

Override the existing Connection handle with the given Connection.

protected void
setTransactionActive(boolean transactionActive)

Set whether this holder represents an active, JDBC-managed transaction.

public boolean
supportsSavepoints()

Return whether JDBC Savepoints are supported.

Inherited from org.springframework.transaction.support.ResourceHolderSupport:
getDeadlinegetTimeToLiveInMillisgetTimeToLiveInSecondshasTimeoutisOpenisRollbackOnlyisSynchronizedWithTransactionisVoidrequestedresetresetRollbackOnlysetRollbackOnlysetSynchronizedWithTransactionsetTimeoutInMillissetTimeoutInSecondsunbound

Field Detail

connectionHandleback to summary
private ConnectionHandle connectionHandle
Annotations
@Nullable
currentConnectionback to summary
private Connection currentConnection
Annotations
@Nullable
SAVEPOINT_NAME_PREFIXback to summary
public static final String SAVEPOINT_NAME_PREFIX

Prefix for savepoint names.

savepointCounterback to summary
private int savepointCounter
savepointsSupportedback to summary
private Boolean savepointsSupported
Annotations
@Nullable
transactionActiveback to summary
private boolean transactionActive

Constructor Detail

ConnectionHolderback to summary
public ConnectionHolder(ConnectionHandle connectionHandle)

Create a new ConnectionHolder for the given ConnectionHandle.

Parameters
connectionHandle:ConnectionHandle

the ConnectionHandle to hold

ConnectionHolderback to summary
public ConnectionHolder(Connection connection)

Create a new ConnectionHolder for the given JDBC Connection, wrapping it with a SimpleConnectionHandle, assuming that there is no ongoing transaction.

Parameters
connection:Connection

the JDBC Connection to hold

See Also
SimpleConnectionHandle, ConnectionHolder(java.sql.Connection, boolean)
ConnectionHolderback to summary
public ConnectionHolder(Connection connection, boolean transactionActive)

Create a new ConnectionHolder for the given JDBC Connection, wrapping it with a SimpleConnectionHandle.

Parameters
connection:Connection

the JDBC Connection to hold

transactionActive:boolean

whether the given Connection is involved in an ongoing transaction

See Also
SimpleConnectionHandle

Method Detail

clearback to summary
public void clear()

Overrides org.springframework.transaction.support.ResourceHolderSupport.clear.

Doc from org.springframework.transaction.support.ResourceHolderSupport.clear.

Clear the transactional state of this resource holder.

Annotations
@Override
createSavepointback to summary
public Savepoint createSavepoint() throws SQLException

Create a new JDBC Savepoint for the current Connection, using generated savepoint names that are unique for the Connection.

Returns:Savepoint

the new Savepoint

Exceptions
SQLException:
if thrown by the JDBC driver
getConnectionback to summary
public Connection getConnection()

Return the current Connection held by this ConnectionHolder.

This will be the same Connection until released gets called on the ConnectionHolder, which will reset the held Connection, fetching a new Connection on demand.

See Also
ConnectionHandle#getConnection(), released()
getConnectionHandleback to summary
public ConnectionHandle getConnectionHandle()

Return the ConnectionHandle held by this ConnectionHolder.

Annotations
@Nullable
hasConnectionback to summary
protected boolean hasConnection()

Return whether this holder currently has a Connection.

isTransactionActiveback to summary
protected boolean isTransactionActive()

Return whether this holder represents an active, JDBC-managed transaction.

releasedback to summary
public void released()

Overrides org.springframework.transaction.support.ResourceHolderSupport.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.

Annotations
@Override
setConnectionback to summary
protected void setConnection(Connection connection)

Override the existing Connection handle with the given Connection. Reset the handle if given null.

Used for releasing the Connection on suspend (with a null argument) and setting a fresh Connection on resume.

setTransactionActiveback to summary
protected void setTransactionActive(boolean transactionActive)

Set whether this holder represents an active, JDBC-managed transaction.

See Also
DataSourceTransactionManager
supportsSavepointsback to summary
public boolean supportsSavepoints() throws SQLException

Return whether JDBC Savepoints are supported. Caches the flag for the lifetime of this ConnectionHolder.

Exceptions
SQLException:
if thrown by the JDBC driver