|
|||||||||
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.AbstractDriverBasedDataSource org.springframework.jdbc.datasource.DriverManagerDataSource org.springframework.jdbc.datasource.SingleConnectionDataSource
public class SingleConnectionDataSource
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.
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
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.
getConnection()
,
Connection.close()
,
DataSourceUtils.releaseConnection(java.sql.Connection, javax.sql.DataSource)
,
NativeJdbcExtractor
Nested Class Summary | |
---|---|
private static class |
SingleConnectionDataSource.CloseSuppressingInvocationHandler
Invocation handler that suppresses close calls on JDBC Connections. |
Field Summary | |
---|---|
private java.lang.Boolean |
autoCommit
Override auto-commit state? |
private java.sql.Connection |
connection
Proxy Connection |
private java.lang.Object |
connectionMonitor
Synchronization monitor for the shared Connection |
private boolean |
suppressClose
Create a close-suppressing proxy? |
private java.sql.Connection |
target
Wrapped Connection |
Fields inherited from class org.springframework.jdbc.datasource.AbstractDataSource |
---|
logger |
Constructor Summary | |
---|---|
SingleConnectionDataSource()
Constructor for bean-style configuration. |
|
SingleConnectionDataSource(java.sql.Connection target,
boolean suppressClose)
Create a new SingleConnectionDataSource with a given Connection. |
|
SingleConnectionDataSource(java.lang.String url,
boolean suppressClose)
Create a new SingleConnectionDataSource with the given standard DriverManager parameters. |
|
SingleConnectionDataSource(java.lang.String url,
java.lang.String username,
java.lang.String password,
boolean suppressClose)
Create a new SingleConnectionDataSource with the given standard DriverManager parameters. |
|
SingleConnectionDataSource(java.lang.String driverClassName,
java.lang.String url,
java.lang.String username,
java.lang.String password,
boolean suppressClose)
Deprecated. since Spring 2.5. Driver parameter usage is generally not recommended for a SingleConnectionDataSource. If you insist on using driver parameters directly, set up the Driver class manually before invoking this DataSource. |
Method Summary | |
---|---|
private void |
closeConnection()
Close the underlying shared Connection. |
void |
destroy()
Close the underlying Connection. |
protected java.lang.Boolean |
getAutoCommitValue()
Return whether the returned Connection's "autoCommit" setting should be overridden. |
protected java.sql.Connection |
getCloseSuppressingConnectionProxy(java.sql.Connection target)
Wrap the given Connection with a proxy that delegates every method call to it but suppresses close calls. |
java.sql.Connection |
getConnection()
This implementation delegates to getConnectionFromDriver ,
using the default username and password of this DataSource. |
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. |
void |
initConnection()
Initialize the underlying Connection via the DriverManager. |
protected boolean |
isSuppressClose()
Return whether the returned Connection will be a close-suppressing proxy or the physical Connection. |
protected void |
prepareConnection(java.sql.Connection con)
Prepare the given Connection before it is exposed. |
void |
resetConnection()
Reset the underlying shared Connection, to be reinitialized on next access. |
void |
setAutoCommit(boolean autoCommit)
Set whether the returned Connection's "autoCommit" setting should be overridden. |
void |
setSuppressClose(boolean suppressClose)
Set whether the returned Connection should be a close-suppressing proxy or the physical Connection. |
boolean |
shouldClose(java.sql.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 |
---|
getConnectionFromDriver, getConnectionFromDriverManager, setDriverClassName |
Methods inherited from class org.springframework.jdbc.datasource.AbstractDriverBasedDataSource |
---|
getConnectionFromDriver, getConnectionProperties, getPassword, getUrl, getUsername, setConnectionProperties, setPassword, setUrl, setUsername |
Methods inherited from class org.springframework.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 |
Methods inherited from interface javax.sql.CommonDataSource |
---|
getLoginTimeout, getLogWriter, setLoginTimeout, setLogWriter |
Methods inherited from interface java.sql.Wrapper |
---|
isWrapperFor, unwrap |
Field Detail |
---|
private boolean suppressClose
private java.lang.Boolean autoCommit
private java.sql.Connection target
private java.sql.Connection connection
private final java.lang.Object connectionMonitor
Constructor Detail |
---|
public SingleConnectionDataSource()
@Deprecated public SingleConnectionDataSource(java.lang.String driverClassName, java.lang.String url, java.lang.String username, java.lang.String password, boolean suppressClose)
driverClassName
- the JDBC driver class nameurl
- the JDBC URL to use for accessing the DriverManagerusername
- the JDBC username to use for accessing the DriverManagerpassword
- the JDBC password to use for accessing the DriverManagersuppressClose
- if the returned Connection should be a
close-suppressing proxy or the physical ConnectionDriverManager.getConnection(String, String, String)
public SingleConnectionDataSource(java.lang.String url, java.lang.String username, java.lang.String password, boolean suppressClose)
url
- the JDBC URL to use for accessing the DriverManagerusername
- the JDBC username to use for accessing the DriverManagerpassword
- the JDBC password to use for accessing the DriverManagersuppressClose
- if the returned Connection should be a
close-suppressing proxy or the physical ConnectionDriverManager.getConnection(String, String, String)
public SingleConnectionDataSource(java.lang.String url, boolean suppressClose)
url
- the JDBC URL to use for accessing the DriverManagersuppressClose
- if the returned Connection should be a
close-suppressing proxy or the physical ConnectionDriverManager.getConnection(String, String, String)
public SingleConnectionDataSource(java.sql.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)
protected boolean isSuppressClose()
public void setAutoCommit(boolean autoCommit)
protected java.lang.Boolean getAutoCommitValue()
null
if none to be appliedpublic java.sql.Connection getConnection() throws java.sql.SQLException
AbstractDriverBasedDataSource
getConnectionFromDriver
,
using the default username and password of this DataSource.
getConnection
in interface javax.sql.DataSource
getConnection
in class AbstractDriverBasedDataSource
java.sql.SQLException
AbstractDriverBasedDataSource.getConnectionFromDriver(String, String)
,
AbstractDriverBasedDataSource.setUsername(java.lang.String)
,
AbstractDriverBasedDataSource.setPassword(java.lang.String)
public java.sql.Connection getConnection(java.lang.String username, java.lang.String password) throws java.sql.SQLException
getConnection
in interface javax.sql.DataSource
getConnection
in class AbstractDriverBasedDataSource
java.sql.SQLException
AbstractDriverBasedDataSource.getConnectionFromDriver(String, String)
public boolean shouldClose(java.sql.Connection con)
shouldClose
in interface SmartDataSource
con
- the Connection to check
Connection.close()
public void destroy()
As this bean implements DisposableBean, a bean factory will automatically invoke this on destruction of its cached singletons.
destroy
in interface DisposableBean
public void initConnection() throws java.sql.SQLException
java.sql.SQLException
public void resetConnection()
protected void prepareConnection(java.sql.Connection con) throws java.sql.SQLException
The default implementation applies the auto-commit flag, if necessary. Can be overridden in subclasses.
con
- the Connection to prepare
java.sql.SQLException
setAutoCommit(boolean)
private void closeConnection()
protected java.sql.Connection getCloseSuppressingConnectionProxy(java.sql.Connection target)
target
- the original Connection to wrap
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |