类 LazyConnectionDataSourceProxy
- java.lang.Object
- org.springframework.jdbc.datasource.DelegatingDataSource
- org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy
- 所有已实现的接口:
Wrapper
,CommonDataSource
,DataSource
,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.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 Java 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. Therefore, the returned Connections cannot be cast to a native JDBC Connection type such as OracleConnection or to a connection pool implementation type. Use a correspondingNativeJdbcExtractor
or JDBC 4'sWrapper.unwrap(java.lang.Class<T>)
to retrieve the native JDBC Connection.- 从以下版本开始:
- 1.1.4
- 作者:
- Juergen Hoeller
- 另请参阅:
DataSourceTransactionManager
构造器概要
构造器 构造器 说明 LazyConnectionDataSourceProxy()
Create a new LazyConnectionDataSourceProxy.LazyConnectionDataSourceProxy(DataSource targetDataSource)
Create a new LazyConnectionDataSourceProxy.
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 void
afterPropertiesSet()
Invoked by the containingBeanFactory
after it has set all bean properties and satisfiedBeanFactoryAware
,ApplicationContextAware
etc.protected void
checkDefaultConnectionProperties(Connection 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.protected Boolean
defaultAutoCommit()
Expose the default auto-commit value.protected Integer
defaultTransactionIsolation()
Expose the default transaction isolation value.Connection
getConnection()
Return a Connection handle that lazily fetches an actual JDBC Connection when asked for a Statement (or PreparedStatement or CallableStatement).Connection
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).void
setDefaultAutoCommit(boolean defaultAutoCommit)
Set the default auto-commit mode to expose when no target Connection has been fetched yet (-> actual JDBC Connection default not known yet).void
setDefaultTransactionIsolation(int defaultTransactionIsolation)
Set the default transaction isolation level to expose when no target Connection has been fetched yet (-> actual JDBC Connection default not known yet).void
setDefaultTransactionIsolationName(String constantName)
Set the default transaction isolation level by the name of the corresponding constant inConnection
, e.g.从类继承的方法 org.springframework.jdbc.datasource.DelegatingDataSource
getLoginTimeout, getLogWriter, getParentLogger, getTargetDataSource, isWrapperFor, setLoginTimeout, setLogWriter, setTargetDataSource, unwrap
从类继承的方法 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
从接口继承的方法 javax.sql.CommonDataSource
createShardingKeyBuilder
从接口继承的方法 javax.sql.DataSource
createConnectionBuilder
构造器详细资料
LazyConnectionDataSourceProxy
public LazyConnectionDataSourceProxy()
Create a new LazyConnectionDataSourceProxy.
LazyConnectionDataSourceProxy
public LazyConnectionDataSourceProxy(DataSource targetDataSource)
Create a new LazyConnectionDataSourceProxy.- 参数:
targetDataSource
- the target DataSource
方法详细资料
setDefaultAutoCommit
public void setDefaultAutoCommit(boolean defaultAutoCommit)
Set the default auto-commit mode to expose when no target Connection has been fetched yet (-> actual JDBC Connection default 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.
setDefaultTransactionIsolation
public void setDefaultTransactionIsolation(int defaultTransactionIsolation)
Set the default transaction isolation level to expose when no target Connection has been fetched yet (-> actual JDBC Connection default 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 (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.
setDefaultTransactionIsolationName
public void setDefaultTransactionIsolationName(String constantName)
Set the default transaction isolation level by the name of the corresponding constant inConnection
, e.g. "TRANSACTION_SERIALIZABLE".- 参数:
constantName
- name of the constant- 另请参阅:
setDefaultTransactionIsolation(int)
,Connection.TRANSACTION_READ_UNCOMMITTED
,Connection.TRANSACTION_READ_COMMITTED
,Connection.TRANSACTION_REPEATABLE_READ
,Connection.TRANSACTION_SERIALIZABLE
afterPropertiesSet
public void afterPropertiesSet()
从接口复制的说明:InitializingBean
Invoked by the containingBeanFactory
after it has set all bean properties and satisfiedBeanFactoryAware
,ApplicationContextAware
etc.This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.
- 指定者:
afterPropertiesSet
在接口中InitializingBean
- 覆盖:
afterPropertiesSet
在类中DelegatingDataSource
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.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.
- 参数:
con
- the Connection to use for checking- 抛出:
SQLException
- if thrown by Connection methods
defaultAutoCommit
protected Boolean defaultAutoCommit()
Expose the default auto-commit value.
defaultTransactionIsolation
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.
- 指定者:
getConnection
在接口中DataSource
- 覆盖:
getConnection
在类中DelegatingDataSource
- 返回:
- a lazy Connection handle
- 抛出:
SQLException
- 另请参阅:
ConnectionProxy.getTargetConnection()
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.
- 指定者:
getConnection
在接口中DataSource
- 覆盖:
getConnection
在类中DelegatingDataSource
- 参数:
username
- the per-Connection usernamepassword
- the per-Connection password- 返回:
- a lazy Connection handle
- 抛出:
SQLException
- 另请参阅:
ConnectionProxy.getTargetConnection()