类 SingleConnectionDataSource
- java.lang.Object
- org.springframework.jdbc.datasource.AbstractDataSource
- org.springframework.jdbc.datasource.AbstractDriverBasedDataSource
- org.springframework.jdbc.datasource.DriverManagerDataSource
- org.springframework.jdbc.datasource.SingleConnectionDataSource
- 所有已实现的接口:
Wrapper
,CommonDataSource
,DataSource
,DisposableBean
,SmartDataSource
public class SingleConnectionDataSource extends DriverManagerDataSource implements SmartDataSource, DisposableBean
Implementation ofSmartDataSource
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. usesDataSourceUtils.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.- 作者:
- Rod Johnson, Juergen Hoeller
- 另请参阅:
getConnection()
,Connection.close()
,DataSourceUtils.releaseConnection(java.sql.Connection, javax.sql.DataSource)
字段概要
从类继承的字段 org.springframework.jdbc.datasource.AbstractDataSource
logger
构造器概要
构造器 构造器 说明 SingleConnectionDataSource()
Constructor 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.
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 void
destroy()
Close the underlying Connection.protected Boolean
getAutoCommitValue()
Return whether the returned Connection's "autoCommit" setting should be overridden.protected Connection
getCloseSuppressingConnectionProxy(Connection target)
Wrap the given Connection with a proxy that delegates every method call to it but suppresses close calls.Connection
getConnection()
This implementation delegates togetConnectionFromDriver
, using the default username and password of this DataSource.Connection
getConnection(String username, 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(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(Connection con)
This is a single Connection: Do not close it when returning to the "pool".从类继承的方法 org.springframework.jdbc.datasource.DriverManagerDataSource
getConnectionFromDriver, getConnectionFromDriverManager, setDriverClassName
从类继承的方法 org.springframework.jdbc.datasource.AbstractDriverBasedDataSource
getCatalog, getConnectionFromDriver, getConnectionProperties, getPassword, getSchema, getUrl, getUsername, setCatalog, setConnectionProperties, setPassword, setSchema, setUrl, setUsername
从类继承的方法 org.springframework.jdbc.datasource.AbstractDataSource
getLoginTimeout, getLogWriter, getParentLogger, isWrapperFor, setLoginTimeout, setLogWriter, unwrap
从类继承的方法 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
从接口继承的方法 javax.sql.CommonDataSource
createShardingKeyBuilder, getParentLogger
从接口继承的方法 javax.sql.DataSource
createConnectionBuilder, getLoginTimeout, getLogWriter, setLoginTimeout, setLogWriter
从接口继承的方法 java.sql.Wrapper
isWrapperFor, unwrap
构造器详细资料
SingleConnectionDataSource
public SingleConnectionDataSource()
Constructor for bean-style configuration.
SingleConnectionDataSource
public SingleConnectionDataSource(String url, String username, String password, boolean suppressClose)
Create a new SingleConnectionDataSource with the given standard DriverManager parameters.- 参数:
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 Connection- 另请参阅:
DriverManager.getConnection(String, String, String)
SingleConnectionDataSource
public SingleConnectionDataSource(String url, boolean suppressClose)
Create a new SingleConnectionDataSource with the given standard DriverManager parameters.- 参数:
url
- the JDBC URL to use for accessing the DriverManagersuppressClose
- if the returned Connection should be a close-suppressing proxy or the physical Connection- 另请参阅:
DriverManager.getConnection(String, String, String)
SingleConnectionDataSource
public SingleConnectionDataSource(Connection target, boolean suppressClose)
Create a new SingleConnectionDataSource with a given Connection.- 参数:
target
- underlying target ConnectionsuppressClose
- if the Connection should be wrapped with a Connection that suppressesclose()
calls (to allow for normalclose()
usage in applications that expect a pooled Connection but do not know our SmartDataSource interface)
方法详细资料
setSuppressClose
public void setSuppressClose(boolean suppressClose)
Set whether the returned Connection should be a close-suppressing proxy or the physical Connection.
isSuppressClose
protected boolean isSuppressClose()
Return whether the returned Connection will be a close-suppressing proxy or the physical Connection.
setAutoCommit
public void setAutoCommit(boolean autoCommit)
Set whether the returned Connection's "autoCommit" setting should be overridden.
getAutoCommitValue
protected Boolean getAutoCommitValue()
Return whether the returned Connection's "autoCommit" setting should be overridden.- 返回:
- the "autoCommit" value, or
null
if none to be applied
getConnection
public Connection getConnection() throws SQLException
从类复制的说明:AbstractDriverBasedDataSource
This implementation delegates togetConnectionFromDriver
, using the default username and password of this DataSource.
getConnection
public Connection getConnection(String username, String password) throws SQLException
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.
shouldClose
public boolean shouldClose(Connection con)
This is a single Connection: Do not close it when returning to the "pool".- 指定者:
shouldClose
在接口中SmartDataSource
- 参数:
con
- the Connection to check- 返回:
- whether the given Connection should be closed
- 另请参阅:
Connection.close()
destroy
public 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 its cached singletons.
- 指定者:
destroy
在接口中DisposableBean
initConnection
public void initConnection() throws SQLException
Initialize the underlying Connection via the DriverManager.- 抛出:
SQLException
resetConnection
public void resetConnection()
Reset the underlying shared Connection, to be reinitialized on next access.
prepareConnection
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.
- 参数:
con
- the Connection to prepare- 抛出:
SQLException
- 另请参阅:
setAutoCommit(boolean)
getCloseSuppressingConnectionProxy
protected Connection getCloseSuppressingConnectionProxy(Connection target)
Wrap the given Connection with a proxy that delegates every method call to it but suppresses close calls.- 参数:
target
- the original Connection to wrap- 返回:
- the wrapped Connection