Class LazyConnectionDataSourceProxy
- All Implemented Interfaces:
- Wrapper,- CommonDataSource,- DataSource,- InitializingBean
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.
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.
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
- See Also:
- 
Constructor SummaryConstructorsConstructorDescriptionCreate a new LazyConnectionDataSourceProxy.LazyConnectionDataSourceProxy(DataSource targetDataSource) Create a new LazyConnectionDataSourceProxy.
- 
Method SummaryModifier and TypeMethodDescriptionvoidInvoked by the containingBeanFactoryafter it has set all bean properties and satisfiedBeanFactoryAware,ApplicationContextAwareetc.protected voidCheck 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.protected BooleanExpose the default auto-commit value.protected IntegerExpose the default transaction isolation value.Return a Connection handle that lazily fetches an actual JDBC Connection when asked for a Statement (or PreparedStatement or CallableStatement).getConnection(String username, String password) Return a Connection handle that lazily fetches an actual JDBC Connection when asked for a Statement (or PreparedStatement or CallableStatement).voidsetDefaultAutoCommit(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).voidsetDefaultTransactionIsolation(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).voidsetDefaultTransactionIsolationName(String constantName) Set the default transaction isolation level by the name of the corresponding constant inConnection, e.g.Methods inherited from class org.springframework.jdbc.datasource.DelegatingDataSourcegetLoginTimeout, getLogWriter, getParentLogger, getTargetDataSource, isWrapperFor, obtainTargetDataSource, setLoginTimeout, setLogWriter, setTargetDataSource, unwrapMethods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface javax.sql.CommonDataSourcecreateShardingKeyBuilderMethods inherited from interface javax.sql.DataSourcecreateConnectionBuilder
- 
Constructor Details- 
LazyConnectionDataSourceProxypublic LazyConnectionDataSourceProxy()Create a new LazyConnectionDataSourceProxy.
- 
LazyConnectionDataSourceProxyCreate a new LazyConnectionDataSourceProxy.- Parameters:
- targetDataSource- the target DataSource
 
 
- 
- 
Method Details- 
setDefaultAutoCommitpublic 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 a target Connection on startup. If that check fails, the default will be determined lazily on first access of a Connection. - See Also:
 
- 
setDefaultTransactionIsolationpublic 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 Connectioninterface; it is mainly intended for programmatic use. Consider using the "defaultTransactionIsolationName" property for setting the value by name (e.g. "TRANSACTION_SERIALIZABLE").If not specified, the default gets determined by checking a target Connection on startup. If that check fails, the default will be determined lazily on first access of a Connection. 
- 
setDefaultTransactionIsolationNameSet the default transaction isolation level by the name of the corresponding constant inConnection, e.g. "TRANSACTION_SERIALIZABLE".- Parameters:
- constantName- name of the constant
- See Also:
 
- 
afterPropertiesSetpublic void afterPropertiesSet()Description copied from interface:InitializingBeanInvoked by the containingBeanFactoryafter it has set all bean properties and satisfiedBeanFactoryAware,ApplicationContextAwareetc.This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set. - Specified by:
- afterPropertiesSetin interface- InitializingBean
- Overrides:
- afterPropertiesSetin class- DelegatingDataSource
 
- 
checkDefaultConnectionPropertiesCheck 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.This will be invoked once on startup, but also for each retrieval of a target Connection. If the check failed on startup (because the database was down), we'll lazily retrieve those settings. - Parameters:
- con- the Connection to use for checking
- Throws:
- SQLException- if thrown by Connection methods
 
- 
defaultAutoCommitExpose the default auto-commit value.
- 
defaultTransactionIsolationExpose the default transaction isolation value.
- 
getConnectionReturn 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:
- getConnectionin interface- DataSource
- Overrides:
- getConnectionin class- DelegatingDataSource
- Returns:
- a lazy Connection handle
- Throws:
- SQLException
- See Also:
 
- 
getConnectionReturn 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:
- getConnectionin interface- DataSource
- Overrides:
- getConnectionin class- DelegatingDataSource
- Parameters:
- username- the per-Connection username
- password- the per-Connection password
- Returns:
- a lazy Connection handle
- Throws:
- SQLException
- See Also:
 
 
-