Top Description Inners Fields Constructors Methods
org.springframework.jdbc.datasource

public Class SingleConnectionDataSource

extends DriverManagerDataSource
implements SmartDataSource, AutoCloseable, DisposableBean
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.DisposableBean, java.lang.AutoCloseable, org.springframework.jdbc.datasource.SmartDataSource, javax.sql.DataSource, java.sql.Wrapper, javax.sql.CommonDataSource
Imports
java.lang.reflect.InvocationHandler, .InvocationTargetException, .Method, .Proxy, java.sql.Connection, .SQLException, org.springframework.beans.factory.DisposableBean, org.springframework.lang.Nullable, org.springframework.util.Assert, .ObjectUtils

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.

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.

Authors
Rod Johnson, Juergen Hoeller
See Also
getConnection(), java.sql.Connection#close(), DataSourceUtils#releaseConnection

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
SingleConnectionDataSource.CloseSuppressingInvocationHandler

Invocation handler that suppresses close calls on JDBC Connections.

Field Summary

Modifier and TypeField and Description
private Boolean
autoCommit

Override auto-commit state?

private Connection
connection

Proxy Connection.

private final Object
connectionMonitor

Synchronization monitor for the shared Connection.

private boolean
rollbackBeforeClose

Explicit rollback before close?

private boolean
suppressClose

Create a close-suppressing proxy?

private Connection
target

Wrapped Connection.

Constructor Summary

AccessConstructor and Description
public
SingleConnectionDataSource()

Constructor for bean-style configuration.

public
SingleConnectionDataSource(String
the JDBC URL to use for accessing the DriverManager
url
,
String
the JDBC username to use for accessing the DriverManager
username
,
String
the JDBC password to use for accessing the DriverManager
password
,
boolean
if the returned Connection should be a close-suppressing proxy or the physical Connection
suppressClose
)

Create a new SingleConnectionDataSource with the given standard DriverManager parameters.

public
SingleConnectionDataSource(String
the JDBC URL to use for accessing the DriverManager
url
,
boolean
if the returned Connection should be a close-suppressing proxy or the physical Connection
suppressClose
)

Create a new SingleConnectionDataSource with the given standard DriverManager parameters.

public
SingleConnectionDataSource(Connection
underlying target Connection
target
,
boolean
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)
suppressClose
)

Create a new SingleConnectionDataSource with a given Connection.

Method Summary

Modifier and TypeMethod and Description
public void
close()

Implements java.lang.AutoCloseable.close.

Close the underlying Connection.

protected void
closeConnection(Connection con)

Close the underlying shared Connection.

public void
destroy()

Implements org.springframework.beans.factory.DisposableBean.destroy.

Close the underlying Connection.

protected Boolean

Returns:

the "autoCommit" value, or null if none to be applied
getAutoCommitValue
()

Return whether the returned Connection's "autoCommit" setting should be overridden.

protected Connection

Returns:

the wrapped Connection
getCloseSuppressingConnectionProxy
(Connection
the original Connection to wrap
target
)

Wrap the given Connection with a proxy that delegates every method call to it but suppresses close calls.

public Connection
getConnection()

Overrides org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection.

Implements javax.sql.DataSource.getConnection.

Attempts to establish a connection with the data source that this DataSource object represents.

public Connection
getConnection(String
the database user on whose behalf the connection is being made
username
,
String
the user's password
password
)

Overrides org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection.

Implements javax.sql.DataSource.getConnection.

Specifying a custom username and password doesn't make sense with a single Connection.

public void
initConnection()

Initialize the underlying Connection via the DriverManager.

protected boolean
isRollbackBeforeClose()

Return whether the shared Connection should be explicitly rolled back before close (if not in auto-commit mode).

protected boolean
isSuppressClose()

Return whether the returned Connection will be a close-suppressing proxy or the physical Connection.

protected void
prepareConnection(Connection
the Connection to prepare
con
)

Prepare the given Connection before it is exposed.

public void
resetConnection()

Reset the underlying shared Connection, to be reinitialized on next access.

public void
setAutoCommit(boolean autoCommit)

Specify whether the returned Connection's "autoCommit" setting should be overridden.

public void
setRollbackBeforeClose(boolean rollbackBeforeClose)

Specify whether the shared Connection should be explicitly rolled back before close (if not in auto-commit mode).

public void
setSuppressClose(boolean suppressClose)

Specify whether the returned Connection should be a close-suppressing proxy or the physical Connection.

public boolean
shouldClose(Connection
the Connection to check
con
)

Implements org.springframework.jdbc.datasource.SmartDataSource.shouldClose.

This is a single Connection: Do not close it when returning to the "pool".

Inherited from org.springframework.jdbc.datasource.DriverManagerDataSource:
getConnectionFromDrivergetConnectionFromDriverManagersetDriverClassName

Field Detail

autoCommitback to summary
private Boolean autoCommit

Override auto-commit state?

Annotations
@Nullable
connectionback to summary
private Connection connection

Proxy Connection.

Annotations
@Nullable
connectionMonitorback to summary
private final Object connectionMonitor

Synchronization monitor for the shared Connection.

rollbackBeforeCloseback to summary
private boolean rollbackBeforeClose

Explicit rollback before close?

suppressCloseback to summary
private boolean suppressClose

Create a close-suppressing proxy?

targetback to summary
private Connection target

Wrapped Connection.

Annotations
@Nullable

Constructor Detail

SingleConnectionDataSourceback to summary
public SingleConnectionDataSource()

Constructor for bean-style configuration.

SingleConnectionDataSourceback to summary
public SingleConnectionDataSource(String url, String username, String password, boolean suppressClose)

Create a new SingleConnectionDataSource with the given standard DriverManager parameters.

Parameters
url:String

the JDBC URL to use for accessing the DriverManager

username:String

the JDBC username to use for accessing the DriverManager

password:String

the JDBC password to use for accessing the DriverManager

suppressClose:boolean

if the returned Connection should be a close-suppressing proxy or the physical Connection

See Also
java.sql.DriverManager#getConnection(String, String, String)
SingleConnectionDataSourceback to summary
public SingleConnectionDataSource(String url, boolean suppressClose)

Create a new SingleConnectionDataSource with the given standard DriverManager parameters.

Parameters
url:String

the JDBC URL to use for accessing the DriverManager

suppressClose:boolean

if the returned Connection should be a close-suppressing proxy or the physical Connection

See Also
java.sql.DriverManager#getConnection(String, String, String)
SingleConnectionDataSourceback to summary
public SingleConnectionDataSource(Connection target, boolean suppressClose)

Create a new SingleConnectionDataSource with a given Connection.

Parameters
target:Connection

underlying target Connection

suppressClose:boolean

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

closeback to summary
public void close()

Implements java.lang.AutoCloseable.close.

Close the underlying Connection. The provider of this DataSource needs to care for proper shutdown.

As this class implements AutoCloseable, it can be used with a try-with-resource statement.

Annotations
@Override
Since
6.1.2
closeConnectionback to summary
protected void closeConnection(Connection con)

Close the underlying shared Connection.

Since
6.1.2
destroyback to summary
public void destroy()

Implements org.springframework.beans.factory.DisposableBean.destroy.

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 the bean.

Annotations
@Override
getAutoCommitValueback to summary
protected Boolean getAutoCommitValue()

Return whether the returned Connection's "autoCommit" setting should be overridden.

Returns:Boolean

the "autoCommit" value, or null if none to be applied

Annotations
@Nullable
getCloseSuppressingConnectionProxyback to summary
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:Connection

the original Connection to wrap

Returns:Connection

the wrapped Connection

getConnectionback to summary
public Connection getConnection() throws SQLException

Overrides org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection.

Implements javax.sql.DataSource.getConnection.

Doc from javax.sql.DataSource.getConnection.

Attempts to establish a connection with the data source that this DataSource object represents.

Returns:Connection

a connection to the data source

Annotations
@Override
Exceptions
SQLException:
if a database access error occurs
getConnectionback to summary
public Connection getConnection(String username, String password) throws SQLException

Overrides org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection.

Implements javax.sql.DataSource.getConnection.

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; throws an SQLException else.

Parameters
username:String

Doc from javax.sql.DataSource.getConnection.

the database user on whose behalf the connection is being made

password:String

Doc from javax.sql.DataSource.getConnection.

the user's password

Returns:Connection

Doc from javax.sql.DataSource.getConnection.

a connection to the data source

Annotations
@Override
Exceptions
SQLException:

Doc from javax.sql.DataSource.getConnection.

if a database access error occurs

initConnectionback to summary
public void initConnection() throws SQLException

Initialize the underlying Connection via the DriverManager.

isRollbackBeforeCloseback to summary
protected boolean isRollbackBeforeClose()

Return whether the shared Connection should be explicitly rolled back before close (if not in auto-commit mode).

Since
6.1.2
isSuppressCloseback to summary
protected boolean isSuppressClose()

Return whether the returned Connection will be a close-suppressing proxy or the physical Connection.

prepareConnectionback to summary
protected void prepareConnection(Connection con) throws SQLException

Prepare the given Connection before it is exposed.

The default implementation applies the auto-commit flag, if necessary. Can be overridden in subclasses.

Parameters
con:Connection

the Connection to prepare

See Also
setAutoCommit
resetConnectionback to summary
public void resetConnection()

Reset the underlying shared Connection, to be reinitialized on next access.

setAutoCommitback to summary
public void setAutoCommit(boolean autoCommit)

Specify whether the returned Connection's "autoCommit" setting should be overridden.

setRollbackBeforeCloseback to summary
public void setRollbackBeforeClose(boolean rollbackBeforeClose)

Specify whether the shared Connection should be explicitly rolled back before close (if not in auto-commit mode).

This is recommended for the Oracle JDBC driver in testing scenarios.

Since
6.1.2
setSuppressCloseback to summary
public void setSuppressClose(boolean suppressClose)

Specify whether the returned Connection should be a close-suppressing proxy or the physical Connection.

shouldCloseback to summary
public boolean shouldClose(Connection con)

Implements org.springframework.jdbc.datasource.SmartDataSource.shouldClose.

This is a single Connection: Do not close it when returning to the "pool".

Parameters
con:Connection

Doc from org.springframework.jdbc.datasource.SmartDataSource.shouldClose.

the Connection to check

Returns:boolean

Doc from org.springframework.jdbc.datasource.SmartDataSource.shouldClose.

whether the given Connection should be closed

Annotations
@Override
org.springframework.jdbc.datasource back to summary

private Class SingleConnectionDataSource.CloseSuppressingInvocationHandler

extends Object
implements InvocationHandler
Class Inheritance
  • java.lang.Object
  • org.springframework.jdbc.datasource.SingleConnectionDataSource.CloseSuppressingInvocationHandler
All Implemented Interfaces
java.lang.reflect.InvocationHandler

Invocation handler that suppresses close calls on JDBC Connections.

Field Summary

Modifier and TypeField and Description
private final Connection

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public Object
invoke(Object
the proxy instance that the method was invoked on
proxy
,
Method
the Method instance corresponding to the interface method invoked on the proxy instance. The declaring class of the Method object will be the interface that the method was declared in, which may be a superinterface of the proxy interface that the proxy class inherits the method through.
method
,
Object[]
an array of objects containing the values of the arguments passed in the method invocation on the proxy instance, or null if interface method takes no arguments. Arguments of primitive types are wrapped in instances of the appropriate primitive wrapper class, such as java.lang.Integer or java.lang.Boolean.
args
)

Implements java.lang.reflect.InvocationHandler.invoke.

Processes a method invocation on a proxy instance and returns the result.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

targetback to summary
private final Connection target

Constructor Detail

CloseSuppressingInvocationHandlerback to summary
public CloseSuppressingInvocationHandler(Connection target)

Method Detail

invokeback to summary
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable

Implements java.lang.reflect.InvocationHandler.invoke.

Doc from java.lang.reflect.InvocationHandler.invoke.

Processes a method invocation on a proxy instance and returns the result. This method will be invoked on an invocation handler when a method is invoked on a proxy instance that it is associated with.

Parameters
proxy:Object

the proxy instance that the method was invoked on

method:Method

the Method instance corresponding to the interface method invoked on the proxy instance. The declaring class of the Method object will be the interface that the method was declared in, which may be a superinterface of the proxy interface that the proxy class inherits the method through.

args:Object[]

an array of objects containing the values of the arguments passed in the method invocation on the proxy instance, or null if interface method takes no arguments. Arguments of primitive types are wrapped in instances of the appropriate primitive wrapper class, such as java.lang.Integer or java.lang.Boolean.

Returns:Object

the value to return from the method invocation on the proxy instance. If the declared return type of the interface method is a primitive type, then the value returned by this method must be an instance of the corresponding primitive wrapper class; otherwise, it must be a type assignable to the declared return type. If the value returned by this method is null and the interface method's return type is primitive, then a NullPointerException will be thrown by the method invocation on the proxy instance. If the value returned by this method is otherwise not compatible with the interface method's declared return type as described above, a ClassCastException will be thrown by the method invocation on the proxy instance.

Annotations
@Override
@Nullable
Exceptions
Throwable:
the exception to throw from the method invocation on the proxy instance. The exception's type must be assignable either to any of the exception types declared in the throws clause of the interface method or to the unchecked exception types java.lang.RuntimeException or java.lang.Error. If a checked exception is thrown by this method that is not assignable to any of the exception types declared in the throws clause of the interface method, then an UndeclaredThrowableException containing the exception that was thrown by this method will be thrown by the method invocation on the proxy instance.