|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.springframework.jdbc.datasource.AbstractDataSource org.springframework.jdbc.datasource.DriverManagerDataSource org.springframework.jdbc.datasource.SingleConnectionDataSource
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 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.
This is primarily a test class. For example, it enables easy testing of code outside 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.
DataSourceUtils.closeConnectionIfNecessary(java.sql.Connection, javax.sql.DataSource)
Field Summary |
Fields inherited from class org.springframework.jdbc.datasource.AbstractDataSource |
logger |
Constructor Summary | |
SingleConnectionDataSource()
Constructor for bean-style configuration. |
|
SingleConnectionDataSource(Connection target,
boolean suppressClose)
Create a new SingleConnectionDataSource with a given connection. |
|
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()
|
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.springframework.jdbc.datasource.DriverManagerDataSource |
getConnectionFromDriverManager, getConnectionFromDriverManager, getDriverClassName, getPassword, getUrl, getUsername, setDriverClassName, setPassword, setUrl, setUsername |
Methods inherited from class org.springframework.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 |
public SingleConnectionDataSource()
public SingleConnectionDataSource(String driverClassName, String url, String username, String password, boolean suppressClose) throws CannotGetJdbcConnectionException
suppressClose
- if the returned connection should be a
close-suppressing proxy or the physical connection.public SingleConnectionDataSource(Connection target, boolean suppressClose)
target
- underlying target connectionsuppressClose
- 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 |
public void setSuppressClose(boolean suppressClose)
public boolean isSuppressClose()
public boolean shouldClose(Connection con)
shouldClose
in interface SmartDataSource
shouldClose
in class DriverManagerDataSource
protected void init() throws SQLException
SQLException
protected void init(Connection target)
target
- the JDBC Connection to usepublic void destroy() throws SQLException
As this bean implements DisposableBean, a bean factory will automatically invoke this on destruction of its cached singletons.
destroy
in interface DisposableBean
SQLException
public Connection getConnection() throws SQLException
getConnection
in interface DataSource
getConnection
in class DriverManagerDataSource
SQLException
public Connection getConnection(String username, String password) throws SQLException
getConnection
in interface DataSource
getConnection
in class DriverManagerDataSource
SQLException
protected Connection getCloseSuppressingConnectionProxy(Connection target)
target
- the original Connection to wrap
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |