Class SingleConnectionDataSource

All Implemented Interfaces:
Wrapper, CommonDataSource, DataSource, DisposableBean, SmartDataSource

public class SingleConnectionDataSource extends DriverManagerDataSource implements SmartDataSource, DisposableBean
Implementation of SmartDataSource that wraps a single JDBC Connection which is not closed after use. Obviously, this is not multi-threading capable.

Note that at shutdown, someone should close the underlying Connection via the close() method. Client code will never call close on the Connection handle if it is SmartDataSource-aware (e.g. uses DataSourceUtils.releaseConnection).

If client code will call close() in the assumption of a pooled Connection, like when using persistence tools, set "suppressClose" to "true". This will return a close-suppressing proxy instead of the physical Connection.

This is primarily intended for testing. For example, it enables easy testing outside an application server, for code that expects to work on a DataSource. In contrast to DriverManagerDataSource, it reuses the same Connection all the time, avoiding excessive creation of physical Connections.

Author:
Rod Johnson, Juergen Hoeller
See Also:
  • Constructor Details

    • SingleConnectionDataSource

      public SingleConnectionDataSource()
      Constructor for bean-style configuration.
    • SingleConnectionDataSource

      public SingleConnectionDataSource(String url, String username, String password, boolean suppressClose)
      Create a new SingleConnectionDataSource with the given standard DriverManager parameters.
      Parameters:
      url - the JDBC URL to use for accessing the DriverManager
      username - the JDBC username to use for accessing the DriverManager
      password - the JDBC password to use for accessing the DriverManager
      suppressClose - if the returned Connection should be a close-suppressing proxy or the physical Connection
      See Also:
    • SingleConnectionDataSource

      public SingleConnectionDataSource(String url, boolean suppressClose)
      Create a new SingleConnectionDataSource with the given standard DriverManager parameters.
      Parameters:
      url - the JDBC URL to use for accessing the DriverManager
      suppressClose - if the returned Connection should be a close-suppressing proxy or the physical Connection
      See Also:
    • SingleConnectionDataSource

      public SingleConnectionDataSource(Connection target, boolean suppressClose)
      Create a new SingleConnectionDataSource with a given Connection.
      Parameters:
      target - underlying target Connection
      suppressClose - if the Connection should be wrapped with a Connection that suppresses close() calls (to allow for normal close() usage in applications that expect a pooled Connection but do not know our SmartDataSource interface)
  • Method Details