|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.springframework.jdbc.datasource.DataSourceUtils
Helper class that provides static methods to obtain connections from JNDI and close connections if necessary. Has support for thread-bound connections, e.g. for use with DataSourceTransactionManager.
Note: The getDataSourceFromJndi
methods are targetted at
applications that do not use a Spring BeanFactory. With the latter, it is
preferable to preconfigure your beans or even JdbcTemplate instances in the
factory: JndiObjectFactoryBean can be used to fetch a DataSource from JNDI
and pass the DataSource bean reference to other beans. Switching to another
DataSource is just a matter of configuration then: For example, you can
replace the definition of the JndiObjectFactoryBean with a local DataSource
(like a Commons DBCP BasicDataSource).
getDataSourceFromJndi(java.lang.String)
,
JndiObjectFactoryBean
,
getConnection(javax.sql.DataSource)
,
DataSourceTransactionManager
Constructor Summary | |
DataSourceUtils()
|
Method Summary | |
static void |
applyTransactionTimeout(java.sql.Statement stmt,
javax.sql.DataSource ds)
Apply the current transaction timeout, if any, to the given JDBC Statement object. |
static void |
closeConnectionIfNecessary(java.sql.Connection con,
javax.sql.DataSource ds)
Close the given Connection if necessary, i.e. if it is not bound to the thread and it is not created by a SmartDataSource returning shouldClose=false. |
protected static void |
doCloseConnectionIfNecessary(java.sql.Connection con,
javax.sql.DataSource ds)
Actually close a JDBC Connection for the given DataSource. |
protected static java.sql.Connection |
doGetConnection(javax.sql.DataSource ds,
boolean allowSynchronization)
Actually get a JDBC Connection for the given DataSource. |
static java.sql.Connection |
getConnection(javax.sql.DataSource ds)
Get a Connection from the given DataSource. |
static java.sql.Connection |
getConnection(javax.sql.DataSource ds,
boolean allowSynchronization)
Get a Connection from the given DataSource. |
static javax.sql.DataSource |
getDataSourceFromJndi(java.lang.String jndiName)
Deprecated. in favor of managing a DataSource via dependency injection, i.e. using a JndiObjectFactoryBean for a JNDI DataSource and pass a bean reference to a setDataSource(DataSource) method or the like |
static javax.sql.DataSource |
getDataSourceFromJndi(java.lang.String jndiName,
boolean resourceRef)
Deprecated. in favor of managing a DataSource via dependency injection, i.e. using a JndiObjectFactoryBean for a JNDI DataSource and pass a bean reference to a setDataSource(DataSource) method or the like |
static java.lang.Integer |
prepareConnectionForTransaction(java.sql.Connection con,
TransactionDefinition definition)
Prepare the given Connection with the given transaction semantics. |
static void |
resetConnectionAfterTransaction(java.sql.Connection con,
java.lang.Integer previousIsolationLevel)
Reset the given Connection after a transaction, regarding read-only flag and isolation level. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public DataSourceUtils()
Method Detail |
public static javax.sql.DataSource getDataSourceFromJndi(java.lang.String jndiName) throws CannotGetJdbcConnectionException
Use getDataSourceFromJndi(jndiName,false) in case of a custom JNDI name.
jndiName
- jndiName of the DataSource
CannotGetJdbcConnectionException
- if the data source cannot be locatedgetDataSourceFromJndi(String, boolean)
,
JndiObjectFactoryBean
public static javax.sql.DataSource getDataSourceFromJndi(java.lang.String jndiName, boolean resourceRef) throws CannotGetJdbcConnectionException
jndiName
- jndiName of the DataSourceresourceRef
- if the lookup occurs in a J2EE container, i.e. if the prefix
"java:comp/env/" needs to be added if the JNDI name doesn't already contain it.
CannotGetJdbcConnectionException
- if the data source cannot be locatedJndiObjectFactoryBean
public static java.sql.Connection getConnection(javax.sql.DataSource ds) throws CannotGetJdbcConnectionException
Is aware of a corresponding Connection bound to the current thread, for example when using DataSourceTransactionManager. Will bind a Connection to the thread if transaction synchronization is active (e.g. if in a JTA transaction).
ds
- DataSource to get Connection from
CannotGetJdbcConnectionException
- if the attempt to get a Connection failedTransactionSynchronizationManager
,
DataSourceTransactionManager
public static java.sql.Connection getConnection(javax.sql.DataSource ds, boolean allowSynchronization) throws CannotGetJdbcConnectionException
Is aware of a corresponding Connection bound to the current thread, for example when using DataSourceTransactionManager. Will bind a Connection to the thread if transaction synchronization is active (e.g. if in a JTA transaction).
ds
- DataSource to get Connection fromallowSynchronization
- if a new JDBC Connection is supposed to be
registered with transaction synchronization (if synchronization is active).
This will always be true for typical data access code.
CannotGetJdbcConnectionException
- if the attempt to get a Connection faileddoGetConnection(javax.sql.DataSource, boolean)
,
TransactionSynchronizationManager
,
DataSourceTransactionManager
protected static java.sql.Connection doGetConnection(javax.sql.DataSource ds, boolean allowSynchronization) throws java.sql.SQLException
Directly accessed by TransactionAwareDataSourceProxy.
java.sql.SQLException
- if thrown by JDBC methodsgetConnection(DataSource, boolean)
,
TransactionAwareDataSourceProxy
public static java.lang.Integer prepareConnectionForTransaction(java.sql.Connection con, TransactionDefinition definition) throws java.sql.SQLException
con
- Connection to preparedefinition
- the transaction definition to apply
java.sql.SQLException
- if thrown by JDBC methodsresetConnectionAfterTransaction(java.sql.Connection, java.lang.Integer)
public static void resetConnectionAfterTransaction(java.sql.Connection con, java.lang.Integer previousIsolationLevel)
con
- Conneciton to resetpreviousIsolationLevel
- the isolation level to restore, if anyprepareConnectionForTransaction(java.sql.Connection, org.springframework.transaction.TransactionDefinition)
public static void applyTransactionTimeout(java.sql.Statement stmt, javax.sql.DataSource ds) throws java.sql.SQLException
stmt
- the JDBC Statement objectds
- DataSource that the Connection came from
java.sql.SQLException
public static void closeConnectionIfNecessary(java.sql.Connection con, javax.sql.DataSource ds)
con
- Connection to close if necessary
(if this is null, the call will be ignored)ds
- DataSource that the Connection came fromSmartDataSource.shouldClose(java.sql.Connection)
protected static void doCloseConnectionIfNecessary(java.sql.Connection con, javax.sql.DataSource ds) throws java.sql.SQLException
Directly accessed by TransactionAwareDataSourceProxy.
java.sql.SQLException
- if thrown by JDBC methodscloseConnectionIfNecessary(java.sql.Connection, javax.sql.DataSource)
,
TransactionAwareDataSourceProxy
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |