Class LazyConnectionDataSourceProxy

java.lang.Object
org.springframework.jdbc.datasource.DelegatingDataSource
org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy
All Implemented Interfaces:
Wrapper, CommonDataSource, DataSource, org.springframework.beans.factory.InitializingBean

public class LazyConnectionDataSourceProxy extends DelegatingDataSource
Proxy for a target DataSource, fetching actual JDBC Connections lazily, i.e. not until first creation of a Statement. Connection initialization properties like auto-commit mode, transaction isolation and read-only mode will be kept and applied to the actual JDBC Connection as soon as an actual Connection is fetched (if ever). Consequently, commit and rollback calls will be ignored if no Statements have been created. As of 6.1.2, there is also special support for a 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. IsolationLevelDataSourceRouter).

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 Wrapper.unwrap(java.lang.Class<T>) to retrieve the native JDBC Connection.

Since:
1.1.4
Author:
Juergen Hoeller, Sam Brannen
See Also:
  • Constructor Details

  • Method Details

    • setReadOnlyDataSource

      public void setReadOnlyDataSource(@Nullable 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 Connection.setReadOnly(boolean) flag will be left untouched, expecting it to be pre-configured as a default on the read-only DataSource, avoiding the overhead of switching it at the beginning and end of every transaction. Also, the default auto-commit and isolation level settings are expected to match the default connection properties of the primary target DataSource.

      Since:
      6.1.2
      See Also:
    • setDefaultAutoCommit

      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.

      See Also:
    • setDefaultTransactionIsolationName

      public void setDefaultTransactionIsolationName(String constantName)
      Set the default transaction isolation level by the name of the corresponding constant in Connection — for example, "TRANSACTION_SERIALIZABLE".
      Parameters:
      constantName - name of the constant
      See Also:
    • setDefaultTransactionIsolation

      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 Connection interface; it is mainly intended for programmatic use. Consider using the "defaultTransactionIsolationName" property for setting the value by name (for example, "TRANSACTION_SERIALIZABLE").

      If not specified, the default gets determined by checking lazily on first access of a Connection.

      See Also:
    • checkDefaultConnectionProperties

      public void checkDefaultConnectionProperties()
      Determine default auto-commit and transaction isolation via a Connection from the target DataSource, if possible.
      Since:
      6.1.2
      See Also:
    • checkDefaultConnectionProperties

      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.
      Parameters:
      con - the Connection to use for checking
      Throws:
      SQLException - if thrown by Connection methods
    • defaultAutoCommit

      @Nullable protected Boolean defaultAutoCommit()
      Expose the default auto-commit value.
    • defaultTransactionIsolation

      @Nullable protected Integer defaultTransactionIsolation()
      Expose the default transaction isolation value.
    • getConnection

      public Connection getConnection() throws SQLException
      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.

      Specified by:
      getConnection in interface DataSource
      Overrides:
      getConnection in class DelegatingDataSource
      Returns:
      a lazy Connection handle
      Throws:
      SQLException
      See Also:
    • getConnection

      public Connection getConnection(String username, String password) throws SQLException
      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.

      Specified by:
      getConnection in interface DataSource
      Overrides:
      getConnection in class DelegatingDataSource
      Parameters:
      username - the per-Connection username
      password - the per-Connection password
      Returns:
      a lazy Connection handle
      Throws:
      SQLException
      See Also: