Package org.springframework.batch.item.database

# Class ExtendedConnectionDataSourceProxy

java.lang.Object
org.springframework.batch.item.database.ExtendedConnectionDataSourceProxy

All Implemented Interfaces:
  `Wrapper`, `CommonDataSource`, `DataSource`, `org.springframework.beans.factory.InitializingBean`, `org.springframework.jdbc.datasource.SmartDataSource`

public class ExtendedConnectionDataSourceProxy extends Object implements org.springframework.jdbc.datasource.SmartDataSource, org.springframework.beans.factory.InitializingBean
Implementation of `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
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(DataSource dataSource)`
  Constructor that takes as a parameter with the `DataSource` to be wrapped.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `afterPropertiesSet()`
  `protected Connection`
  `getCloseSuppressingConnectionProxy(Connection target)`
  Wrap the given Connection with a proxy that delegates every method call to it but suppresses close calls.
  `Connection`
  `getConnection()`
  `Connection`
  `getConnection(String username, String password)`
  `int`
  `getLoginTimeout()`
  `PrintWriter`
  `getLogWriter()`
  `Logger`
  `getParentLogger()`
  Added due to JDK 7 compatibility.
  `boolean`
  `isCloseSuppressionActive(Connection connection)`
  Return the status of close suppression being activated for a given `Connection`
  `boolean`
  `isWrapperFor(Class<?> iface)`
  Performs only a 'shallow' non-recursive check of self's and delegate's class to retain Java 5 compatibility.
  `void`
  `setDataSource(DataSource dataSource)`
  Setter for the `DataSource` that is to be wrapped.
  `void`
  `setLoginTimeout(int seconds)`
  `void`
  `setLogWriter(PrintWriter out)`
  `boolean`
  `shouldClose(Connection connection)`
  `void`
  `startCloseSuppression(Connection connection)`
  `void`
  `stopCloseSuppression(Connection connection)`
  `<T> T`
  `unwrap(Class<T> iface)`
  Returns 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, wait`

  ### Methods inherited from interface javax.sql.CommonDataSource

  `createShardingKeyBuilder`

  ### Methods inherited from interface javax.sql.DataSource

  `createConnectionBuilder`

- ## Constructor Details

  - ### ExtendedConnectionDataSourceProxy

    public ExtendedConnectionDataSourceProxy()
    No arg constructor for use when configured using JavaBean style.

  - ### ExtendedConnectionDataSourceProxy

    public ExtendedConnectionDataSourceProxy(DataSource dataSource)
    Constructor that takes as a parameter with the `DataSource` to be wrapped.
    Parameters:
      `dataSource` - DataSource to be used

- ## Method Details

  - ### setDataSource

    public void setDataSource(DataSource dataSource)
    Setter for the `DataSource` that is to be wrapped.
    Parameters:
      `dataSource` - the DataSource

  - ### shouldClose

    public boolean shouldClose(Connection connection)
    Specified by:
      `shouldClose` in interface `org.springframework.jdbc.datasource.SmartDataSource`
    See Also:
      - `SmartDataSource`

  - ### isCloseSuppressionActive

    public boolean isCloseSuppressionActive(Connection connection)
    Return the status of close suppression being activated for a given `Connection`
    Parameters:
      `connection` - the `Connection` that the close suppression status is requested for
    Returns:
      true or false

  - ### startCloseSuppression

    public void startCloseSuppression(Connection connection)
    Parameters:
      `connection` - the `Connection` that close suppression is requested for

  - ### stopCloseSuppression

    public void stopCloseSuppression(Connection connection)
    Parameters:
      `connection` - the `Connection` that close suppression should be turned off for

  - ### getConnection

    public Connection getConnection() throws SQLException
    Specified by:
      `getConnection` in interface `DataSource`
    Throws:
      `SQLException`

  - ### getConnection

    public Connection getConnection(String username, String password) throws SQLException
    Specified by:
      `getConnection` in interface `DataSource`
    Throws:
      `SQLException`

  - ### getLogWriter

    public PrintWriter getLogWriter() throws SQLException
    Specified by:
      `getLogWriter` in interface `CommonDataSource`
    Specified by:
      `getLogWriter` in interface `DataSource`
    Throws:
      `SQLException`

  - ### getLoginTimeout

    public int getLoginTimeout() throws SQLException
    Specified by:
      `getLoginTimeout` in interface `CommonDataSource`
    Specified by:
      `getLoginTimeout` in interface `DataSource`
    Throws:
      `SQLException`

  - ### setLogWriter

    public void setLogWriter(PrintWriter out) throws SQLException
    Specified by:
      `setLogWriter` in interface `CommonDataSource`
    Specified by:
      `setLogWriter` in interface `DataSource`
    Throws:
      `SQLException`

  - ### setLoginTimeout

    public void setLoginTimeout(int seconds) throws SQLException
    Specified by:
      `setLoginTimeout` in interface `CommonDataSource`
    Specified by:
      `setLoginTimeout` in interface `DataSource`
    Throws:
      `SQLException`

  - ### getCloseSuppressingConnectionProxy

    protected Connection getCloseSuppressingConnectionProxy(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(Class<?> iface) throws SQLException
    Performs only a 'shallow' non-recursive check of self's and delegate's class to retain Java 5 compatibility.
    Specified by:
      `isWrapperFor` in interface `Wrapper`
    Throws:
      `SQLException`

  - ### unwrap

    public <T> T unwrap(Class<T> iface) throws SQLException
    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:
      `unwrap` in interface `Wrapper`
    Throws:
      `SQLException`

  - ### afterPropertiesSet

    public void afterPropertiesSet() throws Exception
    Specified by:
      `afterPropertiesSet` in interface `org.springframework.beans.factory.InitializingBean`
    Throws:
      `Exception`

  - ### getParentLogger

    public Logger getParentLogger() throws SQLFeatureNotSupportedException
    Added due to JDK 7 compatibility.
    Specified by:
      `getParentLogger` in interface `CommonDataSource`
    Throws:
      `SQLFeatureNotSupportedException`