org.compass.gps.device.jdbc.datasource
Class SingleConnectionDataSource

java.lang.Object
  extended by org.compass.gps.device.jdbc.datasource.AbstractDataSource
      extended by org.compass.gps.device.jdbc.datasource.DriverManagerDataSource
          extended by org.compass.gps.device.jdbc.datasource.SingleConnectionDataSource
All Implemented Interfaces:
DataSource

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.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. Be aware that you will not be able to cast this to a native OracleConnection or the like anymore (you need to use a NativeJdbcExtractor for this then).

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.

Taken from Spring

Author:
kimchy
See Also:
Connection.close()

Field Summary
 
Fields inherited from class org.compass.gps.device.jdbc.datasource.AbstractDataSource
log
 
Constructor Summary
SingleConnectionDataSource()
          Constructor for bean-style configuration.
SingleConnectionDataSource(Connection target, boolean suppressClose)
          Create a new SingleConnectionDataSource with a given connection.
SingleConnectionDataSource(String url, boolean suppressClose)
          Create a new SingleConnectionDataSource with the given standard DriverManager parameters.
SingleConnectionDataSource(String url, String username, String password, boolean suppressClose)
          Create a new SingleConnectionDataSource with the given standard DriverManager parameters.
SingleConnectionDataSource(String driverClassName, String url, String username, String password, boolean suppressClose)
          Create a new SingleConnectionDataSource with the given standard DriverManager parameters.
 
Method Summary
 void destroy()
          Close the underlying connection.
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()
          This implementation delegates to getConnectionFromDriverManager, using the default username and password of this DataSource.
 Connection getConnection(String username, String password)
          Specifying a custom username and password doesn't make sense with a single connection.
protected  void init()
          Initialize the underlying connection via DriverManager.
protected  void init(Connection target)
          Initialize 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 should be a close-suppressing proxy or the physical connection.
 boolean shouldClose(Connection con)
          This is a single connection: Do not close it when returning to the "pool".
 
Methods inherited from class org.compass.gps.device.jdbc.datasource.DriverManagerDataSource
getConnectionFromDriverManager, getConnectionFromDriverManager, getDriverClassName, getPassword, getUrl, getUsername, setDriverClassName, setPassword, setUrl, setUsername
 
Methods inherited from class org.compass.gps.device.jdbc.datasource.AbstractDataSource
getLoginTimeout, getLogWriter, isWrapperFor, setLoginTimeout, setLogWriter, unwrap
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SingleConnectionDataSource

public SingleConnectionDataSource()
Constructor for bean-style configuration.


SingleConnectionDataSource

public SingleConnectionDataSource(String driverClassName,
                                  String url,
                                  String username,
                                  String password,
                                  boolean suppressClose)
                           throws CannotGetJdbcConnectionException
Create a new SingleConnectionDataSource with the given standard DriverManager parameters.

Parameters:
driverClassName - the JDBC driver class name
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.
Throws:
CannotGetJdbcConnectionException
See Also:
DriverManager.getConnection(String, String, String)

SingleConnectionDataSource

public SingleConnectionDataSource(String url,
                                  String username,
                                  String password,
                                  boolean suppressClose)
                           throws CannotGetJdbcConnectionException
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.
Throws:
CannotGetJdbcConnectionException
See Also:
DriverManager.getConnection(String, String, String)

SingleConnectionDataSource

public SingleConnectionDataSource(String url,
                                  boolean suppressClose)
                           throws CannotGetJdbcConnectionException
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.
Throws:
CannotGetJdbcConnectionException
See Also:
DriverManager.getConnection(String, String, String)

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 Detail

setSuppressClose

public void setSuppressClose(boolean suppressClose)
Set if the returned connection should 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.


shouldClose

public boolean shouldClose(Connection con)
This is a single connection: Do not close it when returning to the "pool".


init

protected void init()
             throws SQLException
Initialize the underlying connection via DriverManager.

Throws:
SQLException

init

protected void init(Connection target)
Initialize the underlying connection. Wraps the connection with a close-suppressing proxy if necessary.

Parameters:
target - the JDBC Connection to use

destroy

public void destroy()
             throws SQLException
Close the underlying connection. The provider of this DataSource needs to care for proper shutdown.

As this bean implements DisposableBean, a bean factory will automatically invoke this on destruction of its cached singletons.

Throws:
SQLException

getConnection

public Connection getConnection()
                         throws SQLException
Description copied from class: DriverManagerDataSource
This implementation delegates to getConnectionFromDriverManager, using the default username and password of this DataSource.

Specified by:
getConnection in interface DataSource
Overrides:
getConnection in class DriverManagerDataSource
Throws:
SQLException
See Also:
DriverManagerDataSource.getConnectionFromDriverManager()

getConnection

public Connection getConnection(String username,
                                String password)
                         throws 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.

Specified by:
getConnection in interface DataSource
Overrides:
getConnection in class DriverManagerDataSource
Throws:
SQLException
See Also:
DriverManagerDataSource.getConnectionFromDriverManager(String, String, String)

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


Copyright (c) 2004-2008 The Compass Project.