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.
getConnection(), java.sql.Connection#close() , DataSourceUtils#releaseConnection
| Modifier and Type | Class and Description |
|---|---|
| private static class | SingleConnectionDataSource.
Invocation handler that suppresses close calls on JDBC Connections. |
| Modifier and Type | Field 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. |
| Access | Constructor and Description |
|---|---|
| public | |
| 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 suppressClose)close() calls (to allow for normal close()
usage in applications that expect a pooled Connection but do not know our
SmartDataSource interface)Create a new SingleConnectionDataSource with a given Connection. |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| protected void | |
| public void | destroy()
Implements org. Close the underlying Connection. |
| protected Boolean | Returns: the "autoCommit" value, ornull if none to be appliedReturn whether the returned Connection's "autoCommit" setting should be overridden. |
| protected Connection | Returns: the wrapped Connectionthe 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. Implements javax. Attempts to establish a connection with the data source that
this |
| public Connection | getConnection(String
the database user on whose behalf the connection is
being made username, String the user's password password)Overrides org. Implements javax. Specifying a custom username and password doesn't make sense with a single Connection. |
| public void | |
| 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 | |
| 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. This is a single Connection: Do not close it when returning to the "pool". |
| autoCommit | back to summary |
|---|---|
| private Boolean autoCommit Override auto-commit state? | |
| connection | back to summary |
|---|---|
| private Connection connection Proxy Connection. | |
| connectionMonitor | back to summary |
|---|---|
| private final Object connectionMonitor Synchronization monitor for the shared Connection. | |
| rollbackBeforeClose | back to summary |
|---|---|
| private boolean rollbackBeforeClose Explicit rollback before close? | |
| suppressClose | back to summary |
|---|---|
| private boolean suppressClose Create a close-suppressing proxy? | |
| target | back to summary |
|---|---|
| private Connection target Wrapped Connection. | |
| SingleConnectionDataSource | back to summary |
|---|---|
| public SingleConnectionDataSource() Constructor for bean-style configuration. | |
| SingleConnectionDataSource | back to summary |
|---|---|
| public SingleConnectionDataSource(String url, String username, String password, boolean suppressClose) Create a new SingleConnectionDataSource with the given standard DriverManager parameters.
| |
| SingleConnectionDataSource | back to summary |
|---|---|
| public SingleConnectionDataSource(String url, boolean suppressClose) Create a new SingleConnectionDataSource with the given standard DriverManager parameters.
| |
| SingleConnectionDataSource | back to summary |
|---|---|
| public SingleConnectionDataSource(Connection target, boolean suppressClose) Create a new SingleConnectionDataSource with a given Connection.
| |
| close | back to summary |
|---|---|
| public void close() Implements java. Close the underlying Connection. The provider of this DataSource needs to care for proper shutdown. As this class implements
| |
| closeConnection | back to summary |
|---|---|
| protected void closeConnection(Connection con) Close the underlying shared Connection.
| |
| destroy | back to summary |
|---|---|
| public void destroy() Implements org. Close the underlying Connection. The provider of this DataSource needs to care for proper shutdown. As this bean implements
| |
| getAutoCommitValue | back to summary |
|---|---|
| protected Boolean getAutoCommitValue() Return whether the returned Connection's "autoCommit" setting should be overridden. | |
| getCloseSuppressingConnectionProxy | back 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.
| |
| getConnection | back to summary |
|---|---|
| public Connection getConnection() throws SQLException Overrides org. Implements javax. Doc from javax. Attempts to establish a connection with the data source that
this
| |
| getConnection | back to summary |
|---|---|
| public Connection getConnection(String username, String password) throws SQLException Overrides org. Implements javax. 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.
| |
| initConnection | back to summary |
|---|---|
| public void initConnection() throws SQLException Initialize the underlying Connection via the DriverManager. | |
| isRollbackBeforeClose | back to summary |
|---|---|
| protected boolean isRollbackBeforeClose() Return whether the shared Connection should be explicitly rolled back before close (if not in auto-commit mode).
| |
| isSuppressClose | back to summary |
|---|---|
| protected boolean isSuppressClose() Return whether the returned Connection will be a close-suppressing proxy or the physical Connection. | |
| prepareConnection | back 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.
| |
| resetConnection | back to summary |
|---|---|
| public void resetConnection() Reset the underlying shared Connection, to be reinitialized on next access. | |
| setAutoCommit | back to summary |
|---|---|
| public void setAutoCommit(boolean autoCommit) Specify whether the returned Connection's "autoCommit" setting should be overridden. | |
| setRollbackBeforeClose | back 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.
| |
| setSuppressClose | back to summary |
|---|---|
| public void setSuppressClose(boolean suppressClose) Specify whether the returned Connection should be a close-suppressing proxy or the physical Connection. | |
| shouldClose | back to summary |
|---|---|
| public boolean shouldClose(Connection con) Implements org. This is a single Connection: Do not close it when returning to the "pool".
| |
| Modifier and Type | Field and Description |
|---|---|
| private final Connection |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public Object | invoke(Object
the proxy instance that the method was invoked on proxy, Method the method, Object[] 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.an array of objects containing the values of the
arguments passed in the method invocation on the proxy instance,
or args)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.Implements java. Processes a method invocation on a proxy instance and returns the result. |
| target | back to summary |
|---|---|
| private final Connection target | |
| CloseSuppressingInvocationHandler | back to summary |
|---|---|
| public CloseSuppressingInvocationHandler(Connection target) | |
| invoke | back to summary |
|---|---|
| public Object invoke(Object proxy, Method method, Object[] args) throws Throwable Implements java. Doc from java. 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.
| |