com.interface21.jdbc.datasource
Class SingleConnectionDataSource

java.lang.Object
  |
  +--com.interface21.jdbc.datasource.AbstractDataSource
        |
        +--com.interface21.jdbc.datasource.DriverManagerDataSource
              |
              +--com.interface21.jdbc.datasource.SingleConnectionDataSource
All Implemented Interfaces:
javax.sql.DataSource, SmartDataSource

public class SingleConnectionDataSource
extends DriverManagerDataSource

Implementation of SmartDataSource that wraps a single 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.closeConnectionIfNecessary).

If client code will call close in the assumption of a pooled connection, like when using persistence toolkits, set suppressClose to true. This will return a close-suppressing proxy instead of the physical connection. Be aware that you will not be able to cast this to an OracleConnection anymore, for example.

This is primarily a test class. For example, it enables easy testing of code outside of an application server, in conjunction with a mock JNDI InitialContext. 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:
DataSourceUtils.closeConnectionIfNecessary(java.sql.Connection, javax.sql.DataSource), MockInitialContextFactoryBuilder

Fields inherited from class com.interface21.jdbc.datasource.AbstractDataSource
logger
 
Constructor Summary
SingleConnectionDataSource()
          Constructor for bean-style configuration.
SingleConnectionDataSource(java.sql.Connection source, boolean suppressClose)
          Create a new SingleConnectionDataSource with a given connection.
SingleConnectionDataSource(java.lang.String driverName, java.lang.String url, java.lang.String user, java.lang.String password, boolean suppressClose)
          Create a new SingleConnectionDataSource with the given standard DriverManager parameters.
 
Method Summary
 void close()
          Closes the underlying connection.
 java.sql.Connection getConnection()
           
 java.sql.Connection getConnection(java.lang.String username, java.lang.String password)
          Specifying a custom username and password doesn't make sense with a single connection.
protected  void init(java.sql.Connection source)
          Initialized the underlying connection.
 boolean isSuppressClose()
          Return if the returned connection will be a close-suppressing proxy or the physical connection.
 void setSuppressClose(boolean suppressClose)
          Set if the returned connection will be a close-suppressing proxy or the physical connection.
 boolean shouldClose(java.sql.Connection conn)
          This is a single connection: Do not close it when returning to the "pool".
 
Methods inherited from class com.interface21.jdbc.datasource.DriverManagerDataSource
getDriverClassName, getPassword, getUrl, getUsername, setDriverClassName, setPassword, setUrl, setUsername
 
Methods inherited from class com.interface21.jdbc.datasource.AbstractDataSource
getLoginTimeout, getLogWriter, setLoginTimeout, setLogWriter
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface javax.sql.DataSource
getLoginTimeout, getLogWriter, setLoginTimeout, setLogWriter
 

Constructor Detail

SingleConnectionDataSource

public SingleConnectionDataSource()
Constructor for bean-style configuration.

SingleConnectionDataSource

public SingleConnectionDataSource(java.lang.String driverName,
                                  java.lang.String url,
                                  java.lang.String user,
                                  java.lang.String password,
                                  boolean suppressClose)
                           throws CannotGetJdbcConnectionException
Create a new SingleConnectionDataSource with the given standard DriverManager parameters.
Parameters:
suppressClose - if the returned connection will be a close-suppressing proxy or the physical connection.

SingleConnectionDataSource

public SingleConnectionDataSource(java.sql.Connection source,
                                  boolean suppressClose)
                           throws CannotGetJdbcConnectionException,
                                  InvalidDataAccessApiUsageException
Create a new SingleConnectionDataSource with a given connection.
Parameters:
source - underlying source 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 Detail

setSuppressClose

public void setSuppressClose(boolean suppressClose)
Set if the returned connection will be a close-suppressing proxy or the physical connection.

isSuppressClose

public boolean isSuppressClose()
Return if the returned connection will be a close-suppressing proxy or the physical connection.

init

protected void init(java.sql.Connection source)
             throws CannotGetJdbcConnectionException
Initialized the underlying connection.
Parameters:
source - the JDBC Connection to use, or null for initialization via DriverManager

close

public void close()
           throws java.sql.SQLException
Closes the underlying connection. The provider of this DataSource needs to care for proper shutdown.

shouldClose

public boolean shouldClose(java.sql.Connection conn)
This is a single connection: Do not close it when returning to the "pool".
Overrides:
shouldClose in class DriverManagerDataSource
Following copied from interface: com.interface21.jdbc.datasource.SmartDataSource
Parameters:
conn - connection, which should have been obtained from this data source, to check closure status of

getConnection

public java.sql.Connection getConnection()
                                  throws java.sql.SQLException
Overrides:
getConnection in class DriverManagerDataSource

getConnection

public java.sql.Connection getConnection(java.lang.String username,
                                         java.lang.String password)
                                  throws java.sql.SQLException
Specifying a custom username and password doesn't make sense with a single connection. Returns the single connection if given the same username and password, though.
Overrides:
getConnection in class DriverManagerDataSource


Rod Johnson and Spring contributors 2001-2003.