Class ExtendedConnectionDataSourceProxy
- java.lang.Object
- org.springframework.batch.item.database.ExtendedConnectionDataSourceProxy
- All Implemented Interfaces:
java.sql.Wrapper,javax.sql.CommonDataSource,javax.sql.DataSource,org.springframework.beans.factory.InitializingBean,org.springframework.jdbc.datasource.SmartDataSource
public class ExtendedConnectionDataSourceProxy extends java.lang.Object implements org.springframework.jdbc.datasource.SmartDataSource, org.springframework.beans.factory.InitializingBeanImplementation ofSmartDataSourcethat is capable of keeping a single JDBC Connection which is NOT closed after each use even ifConnection.close()is called. The connection can be kept open over multiple transactions when used together with any of Spring'sPlatformTransactionManagerimplementations.Loosely based on the SingleConnectionDataSource implementation in Spring Core. Intended to be used with the
JdbcCursorItemReaderto provide a connection that remains open across transaction boundaries, It remains open for the life of the cursor, and can be shared with the main transaction of the rest of the step processing.Once close suppression has been turned on for a connection, it will be returned for the first
getConnection()call. Any subsequent calls togetConnection()will retrieve a new connection from the wrappedDataSourceuntil theDataSourceUtilsqueries whether the connection should be closed or not by callingshouldClose(Connection)for the close-suppressedConnection. At that point the cycle starts over again, and the nextgetConnection()call will have theConnectionthat is being close-suppressed returned. This allows the use of the close-suppressedConnectionto be the mainConnectionfor an extended data access process. The close suppression is turned off by callingstopCloseSuppression(Connection).This class is not multi-threading capable.
The connection returned will be a close-suppressing proxy instead of the physical
Connection. Be aware that you will not be able to cast this to a nativeOracleConnectionor the like anymore; you'd be required to useWrapper.unwrap(Class).- Since:
- 2.0
- Author:
- Thomas Risberg
- See Also:
getConnection(),Connection.close(),DataSourceUtils.releaseConnection(java.sql.Connection, javax.sql.DataSource),Wrapper.unwrap(Class)
Constructor Summary
Constructors Constructor Description ExtendedConnectionDataSourceProxy()No arg constructor for use when configured using JavaBean style.ExtendedConnectionDataSourceProxy(javax.sql.DataSource dataSource)Constructor that takes as a parameter with theDataSourceto be wrapped.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidafterPropertiesSet()protected java.sql.ConnectiongetCloseSuppressingConnectionProxy(java.sql.Connection target)Wrap the given Connection with a proxy that delegates every method call to it but suppresses close calls.java.sql.ConnectiongetConnection()java.sql.ConnectiongetConnection(java.lang.String username, java.lang.String password)intgetLoginTimeout()java.io.PrintWritergetLogWriter()java.util.logging.LoggergetParentLogger()Added due to JDK 7 compatibility.booleanisCloseSuppressionActive(java.sql.Connection connection)Return the status of close suppression being activated for a givenConnectionbooleanisWrapperFor(java.lang.Class<?> iface)Performs only a 'shallow' non-recursive check of self's and delegate's class to retain Java 5 compatibility.voidsetDataSource(javax.sql.DataSource dataSource)Setter for theDataSourcethat is to be wrapped.voidsetLoginTimeout(int seconds)voidsetLogWriter(java.io.PrintWriter out)booleanshouldClose(java.sql.Connection connection)voidstartCloseSuppression(java.sql.Connection connection)voidstopCloseSuppression(java.sql.Connection connection)<T> Tunwrap(java.lang.Class<T> iface)Returns either self or delegate (in this order) if one of them can be cast to supplied parameter class.
Constructor Detail
ExtendedConnectionDataSourceProxy
public ExtendedConnectionDataSourceProxy()
No arg constructor for use when configured using JavaBean style.
ExtendedConnectionDataSourceProxy
public ExtendedConnectionDataSourceProxy(javax.sql.DataSource dataSource)
Constructor that takes as a parameter with theDataSourceto be wrapped.- Parameters:
dataSource- DataSource to be used
Method Detail
setDataSource
public void setDataSource(javax.sql.DataSource dataSource)
Setter for theDataSourcethat is to be wrapped.- Parameters:
dataSource- the DataSource
shouldClose
public boolean shouldClose(java.sql.Connection connection)
- Specified by:
shouldClosein interfaceorg.springframework.jdbc.datasource.SmartDataSource- See Also:
SmartDataSource
isCloseSuppressionActive
public boolean isCloseSuppressionActive(java.sql.Connection connection)
Return the status of close suppression being activated for a givenConnection- Parameters:
connection- theConnectionthat the close suppression status is requested for- Returns:
- true or false
startCloseSuppression
public void startCloseSuppression(java.sql.Connection connection)
- Parameters:
connection- theConnectionthat close suppression is requested for
stopCloseSuppression
public void stopCloseSuppression(java.sql.Connection connection)
- Parameters:
connection- theConnectionthat close suppression should be turned off for
getConnection
public java.sql.Connection getConnection() throws java.sql.SQLException- Specified by:
getConnectionin interfacejavax.sql.DataSource- Throws:
java.sql.SQLException
getConnection
public java.sql.Connection getConnection(java.lang.String username, java.lang.String password) throws java.sql.SQLException- Specified by:
getConnectionin interfacejavax.sql.DataSource- Throws:
java.sql.SQLException
getLogWriter
public java.io.PrintWriter getLogWriter() throws java.sql.SQLException- Specified by:
getLogWriterin interfacejavax.sql.CommonDataSource- Specified by:
getLogWriterin interfacejavax.sql.DataSource- Throws:
java.sql.SQLException
getLoginTimeout
public int getLoginTimeout() throws java.sql.SQLException- Specified by:
getLoginTimeoutin interfacejavax.sql.CommonDataSource- Specified by:
getLoginTimeoutin interfacejavax.sql.DataSource- Throws:
java.sql.SQLException
setLogWriter
public void setLogWriter(java.io.PrintWriter out) throws java.sql.SQLException- Specified by:
setLogWriterin interfacejavax.sql.CommonDataSource- Specified by:
setLogWriterin interfacejavax.sql.DataSource- Throws:
java.sql.SQLException
setLoginTimeout
public void setLoginTimeout(int seconds) throws java.sql.SQLException- Specified by:
setLoginTimeoutin interfacejavax.sql.CommonDataSource- Specified by:
setLoginTimeoutin interfacejavax.sql.DataSource- Throws:
java.sql.SQLException
getCloseSuppressingConnectionProxy
protected java.sql.Connection getCloseSuppressingConnectionProxy(java.sql.Connection target)
Wrap the given Connection with a proxy that delegates every method call to it but suppresses close calls.- Parameters:
target- the original Connection to wrap- Returns:
- the wrapped Connection
isWrapperFor
public boolean isWrapperFor(java.lang.Class<?> iface) throws java.sql.SQLExceptionPerforms only a 'shallow' non-recursive check of self's and delegate's class to retain Java 5 compatibility.- Specified by:
isWrapperForin interfacejava.sql.Wrapper- Throws:
java.sql.SQLException
unwrap
public <T> T unwrap(java.lang.Class<T> iface) throws java.sql.SQLExceptionReturns either self or delegate (in this order) if one of them can be cast to supplied parameter class. Does *not* support recursive unwrapping of the delegate to retain Java 5 compatibility.- Specified by:
unwrapin interfacejava.sql.Wrapper- Throws:
java.sql.SQLException
afterPropertiesSet
public void afterPropertiesSet() throws java.lang.Exception- Specified by:
afterPropertiesSetin interfaceorg.springframework.beans.factory.InitializingBean- Throws:
java.lang.Exception
getParentLogger
public java.util.logging.Logger getParentLogger() throws java.sql.SQLFeatureNotSupportedExceptionAdded due to JDK 7 compatibility.- Specified by:
getParentLoggerin interfacejavax.sql.CommonDataSource- Throws:
java.sql.SQLFeatureNotSupportedException