open class SingleConnectionDataSource : DriverManagerDataSource, SmartDataSource, DisposableBean
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.
Author
Rod Johnson
Author
Juergen Hoeller
See Also
#getConnection()java.sql.Connection#close()DataSourceUtils#releaseConnection
SingleConnectionDataSource()
Constructor for bean-style configuration. SingleConnectionDataSource(url: String, username: String, password: String, suppressClose: Boolean) SingleConnectionDataSource(url: String, suppressClose: Boolean)
Create a new SingleConnectionDataSource with the given standard DriverManager parameters. SingleConnectionDataSource(target: Connection, suppressClose: Boolean)
Create a new SingleConnectionDataSource with a given Connection. |
open fun destroy(): Unit
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 its cached singletons. |
|
open fun getConnection(): Connection open fun getConnection(username: String, password: String): Connection
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 a SQLException else. |
|
open fun initConnection(): Unit
Initialize the underlying Connection via the DriverManager. |
|
open fun resetConnection(): Unit
Reset the underlying shared Connection, to be reinitialized on next access. |
|
open fun setAutoCommit(autoCommit: Boolean): Unit
Set whether the returned Connection's "autoCommit" setting should be overridden. |
|
open fun setSuppressClose(suppressClose: Boolean): Unit
Set whether the returned Connection should be a close-suppressing proxy or the physical Connection. |
|
open fun shouldClose(con: Connection): Boolean
This is a single Connection: Do not close it when returning to the "pool". |
open fun setDriverClassName(driverClassName: String): Unit
Set the JDBC driver class name. This driver will get initialized on startup, registering itself with the JDK's DriverManager. NOTE: DriverManagerDataSource is primarily intended for accessing pre-registered JDBC drivers. If you need to register a new driver, consider using SimpleDriverDataSource instead. Alternatively, consider initializing the JDBC driver yourself before instantiating this DataSource. The "driverClassName" property is mainly preserved for backwards compatibility, as well as for migrating between Commons DBCP and this DataSource. |