read-only DataSource to use
during a read-only transaction, in addition to the regular target DataSource.
This DataSource proxy allows to avoid fetching JDBC Connections from
a pool unless actually necessary. JDBC transaction control can happen
without fetching a Connection from the pool or communicating with the
database; this will be done lazily on first creation of a JDBC Statement.
As a bonus, this allows for taking the transaction-synchronized read-only
flag and/or isolation level into account in a routing DataSource (e.g.
org.).
If you configure both a LazyConnectionDataSourceProxy and a
TransactionAwareDataSourceProxy, make sure that the latter is the outermost
DataSource. In such a scenario, data access code will talk to the
transaction-aware DataSource, which will in turn work with the
LazyConnectionDataSourceProxy. As of 6.1.2, LazyConnectionDataSourceProxy will
initialize its default connection characteristics on first Connection access;
to enforce this on startup, call checkDefaultConnectionProperties().
Lazy fetching of physical JDBC Connections is particularly beneficial in a generic transaction demarcation environment. It allows you to demarcate transactions on all methods that could potentially perform data access, without paying a performance penalty if no actual data access happens.
This DataSource proxy gives you behavior analogous to JTA and a transactional JNDI DataSource (as provided by the Jakarta EE server), even with a local transaction strategy like DataSourceTransactionManager or HibernateTransactionManager. It does not add value with Spring's JtaTransactionManager as transaction strategy.
Lazy fetching of JDBC Connections is also recommended for read-only operations with Hibernate, in particular if the chances of resolving the result in the second-level cache are high. This avoids the need to communicate with the database at all for such read-only operations. You will get the same effect with non-transactional reads, but lazy fetching of JDBC Connections allows you to still perform reads in transactions.
Note
This DataSource proxy needs to return wrapped Connections
(which implement the ConnectionProxy interface) in order to handle
lazy fetching of an actual JDBC Connection. Use Connection#unwrap
to retrieve the native JDBC Connection.
DataSourceTransactionManager, setTargetDataSource, setReadOnlyDataSource
| Modifier and Type | Class and Description |
|---|---|
| private class | LazyConnectionDataSourceProxy.LazyConnectionInvocationHandler
Invocation handler that defers fetching an actual JDBC Connection until first creation of a Statement. |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv static final Map | constants
Map of constant names to constant values for the isolation constants
defined in |
| private volatile Boolean | |
| private volatile Integer | |
| private static final Log | |
| private DataSource |
| Access | Constructor and Description |
|---|---|
| public | |
| public | LazyConnectionDataSourceProxy(DataSource
the target DataSource targetDataSource)Create a new LazyConnectionDataSourceProxy. |
| Modifier and Type | Method and Description |
|---|---|
| public void | checkDefaultConnectionProperties()
Determine default auto-commit and transaction isolation via a Connection from the target DataSource, if possible. |
| protected void | checkDefaultConnectionProperties(Connection
the Connection to use for checking con)Check the default connection properties (auto-commit, transaction isolation), keeping them to be able to expose them correctly without fetching an actual JDBC Connection from the target DataSource later on. |
| protected Boolean | |
| protected Integer | |
| public Connection | Returns: a lazy Connection handleOverrides org. Implements javax. |
| public Connection | Returns: a lazy Connection handlethe per-Connection username username, String the per-Connection password password)Overrides org. Implements javax. |
| public void | setDefaultAutoCommit(boolean defaultAutoCommit)
Set the default auto-commit mode to expose when no target Connection has been fetched yet (when the actual JDBC Connection default is not known yet). |
| public void | setDefaultTransactionIsolation(int defaultTransactionIsolation)
Set the default transaction isolation level to expose when no target Connection has been fetched yet (when the actual JDBC Connection default is not known yet). |
| public void | setDefaultTransactionIsolationName(String
name of the constant constantName)Set the default transaction isolation level by the name of the corresponding
constant in |
| public void | setReadOnlyDataSource(DataSource readOnlyDataSource)
Specify a variant of the target DataSource to use for read-only transactions. |
| constants | back to summary |
|---|---|
| pack-priv static final Map<String, Integer> constants Map of constant names to constant values for the isolation constants
defined in | |
| defaultAutoCommit | back to summary |
|---|---|
| private volatile Boolean defaultAutoCommit
| |
| defaultTransactionIsolation | back to summary |
|---|---|
| private volatile Integer defaultTransactionIsolation
| |
| logger | back to summary |
|---|---|
| private static final Log logger | |
| readOnlyDataSource | back to summary |
|---|---|
| private DataSource readOnlyDataSource
| |
| LazyConnectionDataSourceProxy | back to summary |
|---|---|
| public LazyConnectionDataSourceProxy() Create a new LazyConnectionDataSourceProxy.
| |
| LazyConnectionDataSourceProxy | back to summary |
|---|---|
| public LazyConnectionDataSourceProxy(DataSource targetDataSource) Create a new LazyConnectionDataSourceProxy.
| |
| checkDefaultConnectionProperties | back to summary |
|---|---|
| public void checkDefaultConnectionProperties() Determine default auto-commit and transaction isolation via a Connection from the target DataSource, if possible.
| |
| checkDefaultConnectionProperties | back to summary |
|---|---|
| protected void checkDefaultConnectionProperties(Connection con) throws SQLException Check the default connection properties (auto-commit, transaction isolation), keeping them to be able to expose them correctly without fetching an actual JDBC Connection from the target DataSource later on.
| |
| defaultAutoCommit | back to summary |
|---|---|
| protected Boolean defaultAutoCommit() Expose the default auto-commit value.
| |
| defaultTransactionIsolation | back to summary |
|---|---|
| protected Integer defaultTransactionIsolation() Expose the default transaction isolation value.
| |
| getConnection | back to summary |
|---|---|
| public Connection getConnection() throws SQLException Overrides org. Implements javax. Return a Connection handle that lazily fetches an actual JDBC Connection when asked for a Statement (or PreparedStatement or CallableStatement). The returned Connection handle implements the ConnectionProxy interface, allowing to retrieve the underlying target Connection.
| |
| getConnection | back to summary |
|---|---|
| public Connection getConnection(String username, String password) throws SQLException Overrides org. Implements javax. Return a Connection handle that lazily fetches an actual JDBC Connection when asked for a Statement (or PreparedStatement or CallableStatement). The returned Connection handle implements the ConnectionProxy interface, allowing to retrieve the underlying target Connection.
| |
| setDefaultAutoCommit | back to summary |
|---|---|
| public void setDefaultAutoCommit(boolean defaultAutoCommit) Set the default auto-commit mode to expose when no target Connection has been fetched yet (when the actual JDBC Connection default is not known yet). If not specified, the default gets determined by checking lazily on first access of a Connection.
| |
| setDefaultTransactionIsolation | back to summary |
|---|---|
| public void setDefaultTransactionIsolation(int defaultTransactionIsolation) Set the default transaction isolation level to expose when no target Connection has been fetched yet (when the actual JDBC Connection default is not known yet). This property accepts the int constant value (e.g. 8) as defined in the
If not specified, the default gets determined by checking lazily on first access of a Connection. | |
| setDefaultTransactionIsolationName | back to summary |
|---|---|
| public void setDefaultTransactionIsolationName(String constantName) Set the default transaction isolation level by the name of the corresponding
constant in
| |
| setReadOnlyDataSource | back to summary |
|---|---|
| public void setReadOnlyDataSource(DataSource readOnlyDataSource) Specify a variant of the target DataSource to use for read-only transactions. If available, a Connection from such a read-only DataSource will be lazily
obtained within a Spring-managed transaction that has been marked as read-only.
The | |
| Modifier and Type | Field and Description |
|---|---|
| private Boolean | |
| private boolean | |
| private int | |
| private String | |
| private boolean | |
| private Connection | |
| private Integer | |
| private String |
| Access | Constructor and Description |
|---|---|
| public | |
| public |
| Modifier and Type | Method and Description |
|---|---|
| private DataSource | |
| private Connection | getTargetConnection(Method operation)
Return the target Connection, fetching it and initializing it if necessary. |
| private boolean | |
| public Object | invoke(Object
the proxy instance that the method was invoked on proxy, Method the method, Object[] 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.an array of objects containing the values of the
arguments passed in the method invocation on the proxy instance,
or args)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.Implements java. |
| autoCommit | back to summary |
|---|---|
| private Boolean autoCommit
| |
| closed | back to summary |
|---|---|
| private boolean closed | |
| holdability | back to summary |
|---|---|
| private int holdability | |
| password | back to summary |
|---|---|
| private String password
| |
| readOnly | back to summary |
|---|---|
| private boolean readOnly | |
| target | back to summary |
|---|---|
| private Connection target
| |
| transactionIsolation | back to summary |
|---|---|
| private Integer transactionIsolation
| |
| username | back to summary |
|---|---|
| private String username
| |
| LazyConnectionInvocationHandler | back to summary |
|---|---|
| public LazyConnectionInvocationHandler() | |
| LazyConnectionInvocationHandler | back to summary |
|---|---|
| public LazyConnectionInvocationHandler(String username, String password) | |
| getDataSourceToUse | back to summary |
|---|---|
| private DataSource getDataSourceToUse() | |
| getTargetConnection | back to summary |
|---|---|
| private Connection getTargetConnection(Method operation) throws SQLException Return the target Connection, fetching it and initializing it if necessary. | |
| hasTargetConnection | back to summary |
|---|---|
| private boolean hasTargetConnection() Return whether the proxy currently holds a target Connection. | |
| invoke | back to summary |
|---|---|
| public Object invoke(Object proxy, Method method, Object[] args) throws Throwable Implements java. Doc from java. 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.
| |