Class ExtendedConnectionDataSourceProxy
- All Implemented Interfaces:
Wrapper,CommonDataSource,DataSource,org.springframework.beans.factory.InitializingBean,org.springframework.jdbc.datasource.SmartDataSource
SmartDataSource that is capable of keeping a single JDBC
Connection which is NOT closed after each use even if Connection.close() is
called.
The connection can be kept open over multiple transactions when used together with any
of Spring's PlatformTransactionManager
implementations.
Loosely based on the SingleConnectionDataSource implementation in Spring Core. Intended
to be used with the JdbcCursorItemReader to 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 to getConnection()
will retrieve a new connection from the wrapped DataSource until the
DataSourceUtils queries whether the connection should be closed or not by
calling shouldClose(Connection) for the close-suppressed Connection.
At that point the cycle starts over again, and the next getConnection() call
will have the Connection that is being close-suppressed returned. This allows
the use of the close-suppressed Connection to be the main Connection
for an extended data access process. The close suppression is turned off by calling
stopCloseSuppression(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 native
OracleConnection or the like anymore; you'd be required to use
Wrapper.unwrap(Class).
- Since:
- 2.0
- Author:
- Thomas Risberg, Mahmoud Ben Hassine, Stefano Cordio
- See Also:
-
getConnection()Connection.close()DataSourceUtils.releaseConnection(java.sql.Connection, javax.sql.DataSource)Wrapper.unwrap(Class)
-
Constructor Summary
ConstructorsConstructorDescriptionNo arg constructor for use when configured using JavaBean style.ExtendedConnectionDataSourceProxy(DataSource dataSource) Constructor that takes as a parameter with theDataSourceto be wrapped. -
Method Summary
Modifier and TypeMethodDescriptionvoidprotected ConnectionWrap the given Connection with a proxy that delegates every method call to it but suppresses close calls.getConnection(String username, String password) intbooleanisCloseSuppressionActive(Connection connection) Return the status of close suppression being activated for a givenConnectionbooleanisWrapperFor(Class<?> iface) Performs only a 'shallow' non-recursive check of self's and delegate's class to retain Java 5 compatibility.voidsetDataSource(DataSource dataSource) Setter for theDataSourcethat is to be wrapped.voidsetLoginTimeout(int seconds) voidsetLogWriter(PrintWriter out) booleanshouldClose(Connection connection) voidstartCloseSuppression(Connection connection) voidstopCloseSuppression(Connection connection) <T> TReturns either self or delegate (in this order) if one of them can be cast to supplied parameter class.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface javax.sql.CommonDataSource
createShardingKeyBuilderMethods inherited from interface javax.sql.DataSource
createConnectionBuilder
-
Constructor Details
-
ExtendedConnectionDataSourceProxy
public ExtendedConnectionDataSourceProxy()No arg constructor for use when configured using JavaBean style. -
ExtendedConnectionDataSourceProxy
Constructor that takes as a parameter with theDataSourceto be wrapped.- Parameters:
dataSource- DataSource to be used
-
-
Method Details
-
setDataSource
Setter for theDataSourcethat is to be wrapped.- Parameters:
dataSource- the DataSource
-
shouldClose
- Specified by:
shouldClosein interfaceorg.springframework.jdbc.datasource.SmartDataSource- See Also:
-
SmartDataSource
-
isCloseSuppressionActive
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
- Parameters:
connection- theConnectionthat close suppression is requested for
-
stopCloseSuppression
- Parameters:
connection- theConnectionthat close suppression should be turned off for
-
getConnection
- Specified by:
getConnectionin interfaceDataSource- Throws:
SQLException
-
getConnection
- Specified by:
getConnectionin interfaceDataSource- Throws:
SQLException
-
getLogWriter
- Specified by:
getLogWriterin interfaceCommonDataSource- Specified by:
getLogWriterin interfaceDataSource- Throws:
SQLException
-
getLoginTimeout
- Specified by:
getLoginTimeoutin interfaceCommonDataSource- Specified by:
getLoginTimeoutin interfaceDataSource- Throws:
SQLException
-
setLogWriter
- Specified by:
setLogWriterin interfaceCommonDataSource- Specified by:
setLogWriterin interfaceDataSource- Throws:
SQLException
-
setLoginTimeout
- Specified by:
setLoginTimeoutin interfaceCommonDataSource- Specified by:
setLoginTimeoutin interfaceDataSource- Throws:
SQLException
-
getCloseSuppressingConnectionProxy
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
Performs only a 'shallow' non-recursive check of self's and delegate's class to retain Java 5 compatibility.- Specified by:
isWrapperForin interfaceWrapper
-
unwrap
Returns 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 interfaceWrapper- Throws:
SQLException
-
afterPropertiesSet
- Specified by:
afterPropertiesSetin interfaceorg.springframework.beans.factory.InitializingBean- Throws:
Exception
-
getParentLogger
- Specified by:
getParentLoggerin interfaceCommonDataSource- Throws:
SQLFeatureNotSupportedException
-