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

public Class TransactionAwareDataSourceProxy

extends DelegatingDataSource
Class Inheritance
Imports
java.lang.reflect.InvocationHandler, .InvocationTargetException, .Method, .Proxy, java.sql.Connection, .SQLException, .Statement, javax.sql.DataSource, org.springframework.lang.Nullable, org.springframework.transaction.support.TransactionSynchronizationManager

Proxy for a target JDBC javax.sql.DataSource, adding awareness of Spring-managed transactions. Similar to a transactional JNDI DataSource as provided by a Jakarta EE server.

Data access code that should remain unaware of Spring's data access support can work with this proxy to seamlessly participate in Spring-managed transactions. Note that the transaction manager, for example DataSourceTransactionManager, still needs to work with the underlying DataSource, not with this proxy.

Make sure that TransactionAwareDataSourceProxy is the outermost DataSource of a chain of DataSource proxies/adapters. TransactionAwareDataSourceProxy can delegate either directly to the target connection pool or to some intermediary proxy/adapter like LazyConnectionDataSourceProxy or UserCredentialsDataSourceAdapter.

Delegates to DataSourceUtils for automatically participating in thread-bound transactions, for example managed by DataSourceTransactionManager. getConnection calls and close calls on returned Connections will behave properly within a transaction, i.e. always operate on the transactional Connection. If not within a transaction, normal DataSource behavior applies.

This proxy allows data access code to work with the plain JDBC API and still participate in Spring-managed transactions, similar to JDBC code in a Jakarta EE/JTA environment. However, if possible, use Spring's DataSourceUtils, JdbcTemplate or JDBC operation objects to get transaction participation even without a proxy for the target DataSource, avoiding the need to define such a proxy in the first place.

As a further effect, using a transaction-aware DataSource will apply remaining transaction timeouts to all created JDBC (Prepared/Callable)Statement. This means that all operations performed through standard JDBC will automatically participate in Spring-managed transaction timeouts.

Note

This DataSource proxy needs to return wrapped Connections (which implement the ConnectionProxy interface) in order to handle close calls properly. Use Connection#unwrap to retrieve the native JDBC Connection.

Author
Juergen Hoeller
Since
1.1
See Also
javax.sql.DataSource#getConnection(), java.sql.Connection#close(), DataSourceUtils#doGetConnection, DataSourceUtils#applyTransactionTimeout, DataSourceUtils#doReleaseConnection

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
TransactionAwareDataSourceProxy.TransactionAwareInvocationHandler

Invocation handler that delegates close calls on JDBC Connections to DataSourceUtils for being aware of thread-bound transactions.

Field Summary

Modifier and TypeField and Description
private boolean
private boolean

Constructor Summary

AccessConstructor and Description
public
TransactionAwareDataSourceProxy()

Create a new TransactionAwareDataSourceProxy.

public
TransactionAwareDataSourceProxy(DataSource
the target DataSource
targetDataSource
)

Create a new TransactionAwareDataSourceProxy.

Method Summary

Modifier and TypeMethod and Description
public Connection

Returns:

a transactional Connection if any, a new one else
getConnection
()

Overrides org.springframework.jdbc.datasource.DelegatingDataSource.getConnection.

Implements javax.sql.DataSource.getConnection.

Delegates to DataSourceUtils for automatically participating in Spring-managed transactions.
protected Connection

Returns:

the wrapped Connection
getTransactionAwareConnectionProxy
(DataSource
the DataSource that the Connection came from
targetDataSource
)

Wraps the given Connection with a proxy that delegates every method call to it but delegates close() calls to DataSourceUtils.

public void
setLazyTransactionalConnections(boolean lazyTransactionalConnections)

Specify whether to obtain the transactional target Connection lazily on actual data access.

public void
setReobtainTransactionalConnections(boolean reobtainTransactionalConnections)

Specify whether to reobtain the target Connection for each operation performed within a transaction.

protected boolean
shouldObtainFixedConnection(DataSource
the target DataSource
targetDataSource
)

Determine whether to obtain a fixed target Connection for the proxy or to reobtain the target Connection for each operation.

Inherited from org.springframework.jdbc.datasource.DelegatingDataSource:
afterPropertiesSetcreateConnectionBuildercreateShardingKeyBuildergetConnectiongetLoginTimeoutgetLogWritergetParentLoggergetTargetDataSourceisWrapperForobtainTargetDataSourcesetLoginTimeoutsetLogWritersetTargetDataSourceunwrap

Field Detail

lazyTransactionalConnectionsback to summary
private boolean lazyTransactionalConnections
reobtainTransactionalConnectionsback to summary
private boolean reobtainTransactionalConnections

Constructor Detail

TransactionAwareDataSourceProxyback to summary
public TransactionAwareDataSourceProxy()

Create a new TransactionAwareDataSourceProxy.

See Also
setTargetDataSource
TransactionAwareDataSourceProxyback to summary
public TransactionAwareDataSourceProxy(DataSource targetDataSource)

Create a new TransactionAwareDataSourceProxy.

Parameters
targetDataSource:DataSource

the target DataSource

Method Detail

getConnectionback to summary
public Connection getConnection() throws SQLException

Overrides org.springframework.jdbc.datasource.DelegatingDataSource.getConnection.

Implements javax.sql.DataSource.getConnection.

Delegates to DataSourceUtils for automatically participating in Spring-managed transactions. Throws the original SQLException, if any.

The returned Connection handle implements the ConnectionProxy interface, allowing to retrieve the underlying target Connection.

Returns:Connection

a transactional Connection if any, a new one else

Annotations
@Override
Exceptions
SQLException:

Doc from javax.sql.DataSource.getConnection.

if a database access error occurs

See Also
DataSourceUtils#doGetConnection, ConnectionProxy#getTargetConnection
getTransactionAwareConnectionProxyback to summary
protected Connection getTransactionAwareConnectionProxy(DataSource targetDataSource)

Wraps the given Connection with a proxy that delegates every method call to it but delegates close() calls to DataSourceUtils.

Parameters
targetDataSource:DataSource

the DataSource that the Connection came from

Returns:Connection

the wrapped Connection

See Also
java.sql.Connection#close(), DataSourceUtils#doReleaseConnection
setLazyTransactionalConnectionsback to summary
public void setLazyTransactionalConnections(boolean lazyTransactionalConnections)

Specify whether to obtain the transactional target Connection lazily on actual data access.

The default is "true". Specify "false" to immediately obtain a target Connection when a transaction-aware Connection handle is retrieved.

Since
6.1.2
setReobtainTransactionalConnectionsback to summary
public void setReobtainTransactionalConnections(boolean reobtainTransactionalConnections)

Specify whether to reobtain the target Connection for each operation performed within a transaction.

The default is "false". Specify "true" to reobtain transactional Connections for every call on the Connection proxy; this is advisable on JBoss if you hold on to a Connection handle across transaction boundaries.

The effect of this setting is similar to the "hibernate.connection.release_mode" value "after_statement".

shouldObtainFixedConnectionback to summary
protected boolean shouldObtainFixedConnection(DataSource targetDataSource)

Determine whether to obtain a fixed target Connection for the proxy or to reobtain the target Connection for each operation.

The default implementation returns true for all standard cases. This can be overridden through the "reobtainTransactionalConnections" flag, which enforces a non-fixed target Connection within an active transaction. Note that non-transactional access will always use a fixed Connection.

Parameters
targetDataSource:DataSource

the target DataSource

org.springframework.jdbc.datasource back to summary

private Class TransactionAwareDataSourceProxy.TransactionAwareInvocationHandler

extends Object
implements InvocationHandler
Class Inheritance
  • java.lang.Object
  • org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy.TransactionAwareInvocationHandler
All Implemented Interfaces
java.lang.reflect.InvocationHandler

Invocation handler that delegates close calls on JDBC Connections to DataSourceUtils for being aware of thread-bound transactions.

Field Summary

Modifier and TypeField and Description
private boolean
private Connection
private final DataSource

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public Object
invoke(Object
the proxy instance that the method was invoked on
proxy
,
Method
the Method instance corresponding to the interface method invoked on the proxy instance. The declaring class of the Method object will be the interface that the method was declared in, which may be a superinterface of the proxy interface that the proxy class inherits the method through.
method
,
Object[]
an array of objects containing the values of the arguments passed in the method invocation on the proxy instance, or null if interface method takes no arguments. Arguments of primitive types are wrapped in instances of the appropriate primitive wrapper class, such as java.lang.Integer or java.lang.Boolean.
args
)

Implements java.lang.reflect.InvocationHandler.invoke.

Processes a method invocation on a proxy instance and returns the result.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

closedback to summary
private boolean closed
targetback to summary
private Connection target
Annotations
@Nullable
targetDataSourceback to summary
private final DataSource targetDataSource

Constructor Detail

TransactionAwareInvocationHandlerback to summary
public TransactionAwareInvocationHandler(DataSource targetDataSource)

Method Detail

invokeback to summary
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable

Implements java.lang.reflect.InvocationHandler.invoke.

Doc from java.lang.reflect.InvocationHandler.invoke.

Processes a method invocation on a proxy instance and returns the result. This method will be invoked on an invocation handler when a method is invoked on a proxy instance that it is associated with.

Parameters
proxy:Object

the proxy instance that the method was invoked on

method:Method

the Method instance corresponding to the interface method invoked on the proxy instance. The declaring class of the Method object will be the interface that the method was declared in, which may be a superinterface of the proxy interface that the proxy class inherits the method through.

args:Object[]

an array of objects containing the values of the arguments passed in the method invocation on the proxy instance, or null if interface method takes no arguments. Arguments of primitive types are wrapped in instances of the appropriate primitive wrapper class, such as java.lang.Integer or java.lang.Boolean.

Returns:Object

the value to return from the method invocation on the proxy instance. If the declared return type of the interface method is a primitive type, then the value returned by this method must be an instance of the corresponding primitive wrapper class; otherwise, it must be a type assignable to the declared return type. If the value returned by this method is null and the interface method's return type is primitive, then a NullPointerException will be thrown by the method invocation on the proxy instance. If the value returned by this method is otherwise not compatible with the interface method's declared return type as described above, a ClassCastException will be thrown by the method invocation on the proxy instance.

Annotations
@Override
@Nullable
Exceptions
Throwable:
the exception to throw from the method invocation on the proxy instance. The exception's type must be assignable either to any of the exception types declared in the throws clause of the interface method or to the unchecked exception types java.lang.RuntimeException or java.lang.Error. If a checked exception is thrown by this method that is not assignable to any of the exception types declared in the throws clause of the interface method, then an UndeclaredThrowableException containing the exception that was thrown by this method will be thrown by the method invocation on the proxy instance.