org.springframework.jdbc.datasource
Class SingleConnectionDataSource

org.springframework.jdbc.datasource.AbstractDataSource
  |
  +--org.springframework.jdbc.datasource.DriverManagerDataSource
        |
        +--org.springframework.jdbc.datasource.SingleConnectionDataSource

public class SingleConnectionDataSource
extends DriverManagerDataSource
implements DisposableBean

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 simple JNDI environment. 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), SimpleNamingContextBuilder

Constructor Summary
SingleConnectionDataSource()
          Constructor for bean-style configuration.
SingleConnectionDataSource(String driverClassName, String url, String username, String password, boolean suppressClose)
          Create a new SingleConnectionDataSource with the given standard DriverManager parameters.
SingleConnectionDataSource(Connection source, boolean suppressClose)
          Create a new SingleConnectionDataSource with a given connection.

Method Summary
 voiddestroy()
          Close the underlying connection.
 ConnectiongetConnection()
           
 ConnectiongetConnection(String username, String password)
          Specifying a custom username and password doesn't make sense with a single connection.
 booleanisSuppressClose()
          Return if the returned connection will be a close-suppressing proxy or the physical connection.
 voidsetSuppressClose(boolean suppressClose)
          Set if the returned connection will be a close-suppressing proxy or the physical connection.
 booleanshouldClose(Connection conn)
          This is a single connection: Do not close it when returning to the "pool".

Methods inherited from class org.springframework.jdbc.datasource.DriverManagerDataSource
getDriverClassName, getPassword, getUrl, getUsername, setDriverClassName, setPassword, setUrl, setUsername

Methods inherited from class org.springframework.jdbc.datasource.AbstractDataSource
getLoginTimeout, getLogWriter, setLoginTimeout, setLogWriter

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:
suppressClose - if the returned connection will be a close-suppressing proxy or the physical connection.

SingleConnectionDataSource

public SingleConnectionDataSource(Connection source, boolean suppressClose)
throws CannotGetJdbcConnectionException,
org.springframework.dao.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

destroy

public void destroy()
throws java.sql.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.


getConnection

public Connection getConnection()
throws java.sql.SQLException

getConnection

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

isSuppressClose

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

setSuppressClose

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

shouldClose

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

Association Links

to Class java.sql.Connection

wrapped connection