public class ExtendedConnectionDataSourceProxy
extends java.lang.Object
implements org.springframework.jdbc.datasource.SmartDataSource, org.springframework.beans.factory.InitializingBean
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).
getConnection(),
Connection.close(),
DataSourceUtils.releaseConnection(java.sql.Connection, javax.sql.DataSource),
Wrapper.unwrap(Class)| Constructor and Description |
|---|
ExtendedConnectionDataSourceProxy()
No arg constructor for use when configured using JavaBean style.
|
ExtendedConnectionDataSourceProxy(javax.sql.DataSource dataSource)
Constructor that takes as a parameter with the
DataSource to be
wrapped. |
| Modifier and Type | Method and Description |
|---|---|
void |
afterPropertiesSet() |
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.
|
java.sql.Connection |
getConnection() |
java.sql.Connection |
getConnection(java.lang.String username,
java.lang.String password) |
int |
getLoginTimeout() |
java.io.PrintWriter |
getLogWriter() |
java.util.logging.Logger |
getParentLogger()
Added due to JDK 7 compatibility.
|
boolean |
isCloseSuppressionActive(java.sql.Connection connection)
Return the status of close suppression being activated for a given
Connection |
boolean |
isWrapperFor(java.lang.Class<?> iface)
Performs only a 'shallow' non-recursive check of self's and delegate's
class to retain Java 5 compatibility.
|
void |
setDataSource(javax.sql.DataSource dataSource)
Setter for the
DataSource that is to be wrapped. |
void |
setLoginTimeout(int seconds) |
void |
setLogWriter(java.io.PrintWriter out) |
boolean |
shouldClose(java.sql.Connection connection) |
void |
startCloseSuppression(java.sql.Connection connection) |
void |
stopCloseSuppression(java.sql.Connection connection) |
<T> T |
unwrap(java.lang.Class<T> iface)
Returns either self or delegate (in this order) if one of them can be
cast to supplied parameter class.
|
public ExtendedConnectionDataSourceProxy()
public ExtendedConnectionDataSourceProxy(javax.sql.DataSource dataSource)
DataSource to be
wrapped.dataSource - DataSource to be usedpublic void setDataSource(javax.sql.DataSource dataSource)
DataSource that is to be wrapped.dataSource - the DataSourcepublic boolean shouldClose(java.sql.Connection connection)
shouldClose in interface org.springframework.jdbc.datasource.SmartDataSourceSmartDataSourcepublic boolean isCloseSuppressionActive(java.sql.Connection connection)
Connectionconnection - the Connection that the close suppression
status is requested forpublic void startCloseSuppression(java.sql.Connection connection)
connection - the Connection that close suppression is
requested forpublic void stopCloseSuppression(java.sql.Connection connection)
connection - the Connection that close suppression should be
turned off forpublic java.sql.Connection getConnection()
throws java.sql.SQLException
getConnection in interface javax.sql.DataSourcejava.sql.SQLExceptionpublic java.sql.Connection getConnection(java.lang.String username,
java.lang.String password)
throws java.sql.SQLException
getConnection in interface javax.sql.DataSourcejava.sql.SQLExceptionpublic java.io.PrintWriter getLogWriter()
throws java.sql.SQLException
getLogWriter in interface javax.sql.CommonDataSourcejava.sql.SQLExceptionpublic int getLoginTimeout()
throws java.sql.SQLException
getLoginTimeout in interface javax.sql.CommonDataSourcejava.sql.SQLExceptionpublic void setLogWriter(java.io.PrintWriter out)
throws java.sql.SQLException
setLogWriter in interface javax.sql.CommonDataSourcejava.sql.SQLExceptionpublic void setLoginTimeout(int seconds)
throws java.sql.SQLException
setLoginTimeout in interface javax.sql.CommonDataSourcejava.sql.SQLExceptionprotected java.sql.Connection getCloseSuppressingConnectionProxy(java.sql.Connection target)
target - the original Connection to wrappublic boolean isWrapperFor(java.lang.Class<?> iface)
throws java.sql.SQLException
isWrapperFor in interface java.sql.Wrapperjava.sql.SQLExceptionpublic <T> T unwrap(java.lang.Class<T> iface)
throws java.sql.SQLException
unwrap in interface java.sql.Wrapperjava.sql.SQLExceptionpublic void afterPropertiesSet()
throws java.lang.Exception
afterPropertiesSet in interface org.springframework.beans.factory.InitializingBeanjava.lang.Exceptionpublic java.util.logging.Logger getParentLogger()
throws java.sql.SQLFeatureNotSupportedException
getParentLogger in interface javax.sql.CommonDataSourcejava.sql.SQLFeatureNotSupportedException