Class SingleConnectionDataSource
- All Implemented Interfaces:
- AutoCloseable, Wrapper, CommonDataSource, DataSource, DisposableBean, SmartDataSource
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 (for example, 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.
- Author:
- Rod Johnson, Juergen Hoeller
- See Also:
- 
Field SummaryFields inherited from class AbstractDataSourcelogger
- 
Constructor SummaryConstructorsConstructorDescriptionConstructor for bean-style configuration.SingleConnectionDataSource(String url, boolean suppressClose) Create a new SingleConnectionDataSource with the given standard DriverManager parameters.SingleConnectionDataSource(String url, String username, String password, boolean suppressClose) Create a new SingleConnectionDataSource with the given standard DriverManager parameters.SingleConnectionDataSource(Connection target, boolean suppressClose) Create a new SingleConnectionDataSource with a given Connection.
- 
Method SummaryModifier and TypeMethodDescriptionvoidclose()Close the underlying Connection.protected voidClose the underlying shared Connection.voiddestroy()Close the underlying Connection.Return whether the returned Connection's "autoCommit" setting should be overridden.protected ConnectionWrap the given Connection with a proxy that delegates every method call to it but suppresses close calls.This implementation delegates togetConnectionFromDriver, using the default username and password of this DataSource.getConnection(String username, String password) Specifying a custom username and password doesn't make sense with a single Connection.voidInitialize the underlying Connection via the DriverManager.protected booleanReturn whether the shared Connection should be explicitly rolled back before close (if not in auto-commit mode).protected booleanReturn whether the returned Connection will be a close-suppressing proxy or the physical Connection.protected voidPrepare the given Connection before it is exposed.voidReset the underlying shared Connection, to be reinitialized on next access.voidsetAutoCommit(boolean autoCommit) Specify whether the returned Connection's "autoCommit" setting should be overridden.voidsetRollbackBeforeClose(boolean rollbackBeforeClose) Specify whether the shared Connection should be explicitly rolled back before close (if not in auto-commit mode).voidsetSuppressClose(boolean suppressClose) Specify whether the returned Connection should be a close-suppressing proxy or the physical Connection.booleanshouldClose(Connection con) This is a single Connection: Do not close it when returning to the "pool".Methods inherited from class DriverManagerDataSourcegetConnectionFromDriver, getConnectionFromDriverManager, setDriverClassNameMethods inherited from class AbstractDriverBasedDataSourcegetCatalog, getConnectionFromDriver, getConnectionProperties, getPassword, getSchema, getUrl, getUsername, setCatalog, setConnectionProperties, setPassword, setSchema, setUrl, setUsernameMethods inherited from class AbstractDataSourcegetLoginTimeout, getLogWriter, getParentLogger, isWrapperFor, setLoginTimeout, setLogWriter, unwrapMethods inherited from class Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface CommonDataSourcecreateShardingKeyBuilder, getParentLoggerMethods inherited from interface DataSourcecreateConnectionBuilder, getLoginTimeout, getLogWriter, setLoginTimeout, setLogWriterMethods inherited from interface WrapperisWrapperFor, unwrap
- 
Constructor Details- 
SingleConnectionDataSourcepublic SingleConnectionDataSource()Constructor for bean-style configuration.
- 
SingleConnectionDataSourcepublic SingleConnectionDataSource(String url, String username, String password, boolean suppressClose) Create a new SingleConnectionDataSource with the given standard DriverManager parameters.- Parameters:
- url- the JDBC URL to use for accessing the DriverManager
- username- the JDBC username to use for accessing the DriverManager
- password- the JDBC password to use for accessing the DriverManager
- suppressClose- if the returned Connection should be a close-suppressing proxy or the physical Connection
- See Also:
 
- 
SingleConnectionDataSourceCreate a new SingleConnectionDataSource with the given standard DriverManager parameters.- Parameters:
- url- the JDBC URL to use for accessing the DriverManager
- suppressClose- if the returned Connection should be a close-suppressing proxy or the physical Connection
- See Also:
 
- 
SingleConnectionDataSourceCreate a new SingleConnectionDataSource with a given Connection.- Parameters:
- target- underlying target 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 Details- 
setSuppressClosepublic void setSuppressClose(boolean suppressClose) Specify whether the returned Connection should be a close-suppressing proxy or the physical Connection.
- 
isSuppressCloseprotected boolean isSuppressClose()Return whether the returned Connection will be a close-suppressing proxy or the physical Connection.
- 
setRollbackBeforeClosepublic 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
 
- 
isRollbackBeforeCloseprotected boolean isRollbackBeforeClose()Return whether the shared Connection should be explicitly rolled back before close (if not in auto-commit mode).- Since:
- 6.1.2
 
- 
setAutoCommitpublic void setAutoCommit(boolean autoCommit) Specify whether the returned Connection's "autoCommit" setting should be overridden.
- 
getAutoCommitValue
- 
getConnectionDescription copied from class:AbstractDriverBasedDataSourceThis implementation delegates togetConnectionFromDriver, using the default username and password of this DataSource.- Specified by:
- getConnectionin interface- DataSource
- Overrides:
- getConnectionin class- AbstractDriverBasedDataSource
- Throws:
- SQLException
- See Also:
 
- 
getConnectionSpecifying 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.- Specified by:
- getConnectionin interface- DataSource
- Overrides:
- getConnectionin class- AbstractDriverBasedDataSource
- Throws:
- SQLException
- See Also:
 
- 
shouldCloseThis is a single Connection: Do not close it when returning to the "pool".- Specified by:
- shouldClosein interface- SmartDataSource
- Parameters:
- con- the Connection to check
- Returns:
- whether the given Connection should be closed
- See Also:
 
- 
closepublic void 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.- Specified by:
- closein interface- AutoCloseable
- Since:
- 6.1.2
 
- 
destroypublic void 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.- Specified by:
- destroyin interface- DisposableBean
 
- 
initConnectionInitialize the underlying Connection via the DriverManager.- Throws:
- SQLException
 
- 
resetConnectionpublic void resetConnection()Reset the underlying shared Connection, to be reinitialized on next access.
- 
prepareConnectionPrepare the given Connection before it is exposed.The default implementation applies the auto-commit flag, if necessary. Can be overridden in subclasses. - Parameters:
- con- the Connection to prepare
- Throws:
- SQLException
- See Also:
 
- 
closeConnection
- 
getCloseSuppressingConnectionProxyWrap the given Connection with a proxy that delegates every method call to it but suppresses close calls.- Parameters:
- target- the original Connection to wrap
- Returns:
- the wrapped Connection
 
 
-