com.interface21.jdbc.datasource
Class DataSourceUtils

java.lang.Object
  |
  +--com.interface21.jdbc.datasource.DataSourceUtils

public abstract class DataSourceUtils
extends java.lang.Object

Class containing static methods to obtain connections from JNDI and close connections if necessary. Has support for thread-bound connections, for example for using DataSourceTransactionManager.

Note: The getDataSourceFromJndi methods are targetted at applications that do not use a BeanFactory resp. an ApplicationContext. 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 give the DataSource bean reference to other beans. Switching to another DataSource is just a matter of configuration then: You can even replace the definition of the FactoryBean with a non-JNDI DataSource!

Version:
$Id: DataSourceUtils.java,v 1.13 2003/06/06 14:34:17 jhoeller Exp $
Author:
Rod Johnson, Juergen Hoeller
See Also:
DataSourceTransactionManager, JndiObjectFactoryBean

Constructor Summary
DataSourceUtils()
           
 
Method Summary
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.
static java.sql.Connection getCloseSuppressingConnectionProxy(java.sql.Connection source)
          Wrap the given connection with a proxy that delegates every method call to it but suppresses close calls.
static java.sql.Connection getConnection(javax.sql.DataSource ds)
          Get a connection from the given DataSource.
static javax.sql.DataSource getDataSourceFromJndi(java.lang.String jndiName)
          Look up the specified DataSource in JNDI, assuming that the lookup occurs in a J2EE container, i.e. adding the prefix "java:comp/env/" to the JNDI name if it doesn't already contain it.
static javax.sql.DataSource getDataSourceFromJndi(java.lang.String jndiName, boolean inContainer)
          Look up the specified DataSource in JNDI, explicitly specifying if the lookup occurs in a J2EE container.
static ThreadObjectManager getThreadObjectManager()
          Return the thread object manager for data sources, keeping a DataSource/ConnectionHolder map per thread for JDBC transactions.
static boolean isConnectionBoundToThread(java.sql.Connection con, javax.sql.DataSource ds)
          Return if the given Connection is bound to the current thread, for the given DataSource.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DataSourceUtils

public DataSourceUtils()
Method Detail

getThreadObjectManager

public static ThreadObjectManager getThreadObjectManager()
Return the thread object manager for data sources, keeping a DataSource/ConnectionHolder map per thread for JDBC transactions.
Returns:
the thread object manager
See Also:
getConnection(javax.sql.DataSource), DataSourceTransactionManager

isConnectionBoundToThread

public static boolean isConnectionBoundToThread(java.sql.Connection con,
                                                javax.sql.DataSource ds)
Return if the given Connection is bound to the current thread, for the given DataSource.
Parameters:
con - JDBC Connection that should be checked
ds - DataSource that the Connection was created with
Returns:
if the Connection is bound for the DataSource

getDataSourceFromJndi

public static javax.sql.DataSource getDataSourceFromJndi(java.lang.String jndiName)
                                                  throws CannotGetJdbcConnectionException
Look up the specified DataSource in JNDI, assuming that the lookup occurs in a J2EE container, i.e. adding the prefix "java:comp/env/" to the JNDI name if it doesn't already contain it.

Use getDataSourceFromJndi(jndiName,false) in case of a custom JNDI name.

Parameters:
jndiName - jndiName of the DataSource
Returns:
the DataSource
Throws:
CannotGetJdbcConnectionException - if the data source cannot be located
See Also:
getDataSourceFromJndi(String,boolean)

getDataSourceFromJndi

public static javax.sql.DataSource getDataSourceFromJndi(java.lang.String jndiName,
                                                         boolean inContainer)
                                                  throws CannotGetJdbcConnectionException
Look up the specified DataSource in JNDI, explicitly specifying if the lookup occurs in a J2EE container.
Parameters:
jndiName - jndiName of the DataSource
inContainer - 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.
Returns:
the DataSource
Throws:
CannotGetJdbcConnectionException - if the data source cannot be located

getConnection

public static java.sql.Connection getConnection(javax.sql.DataSource ds)
                                         throws CannotGetJdbcConnectionException
Get a connection from the given DataSource. Changes any SQL exception into the Spring hierarchy of unchecked generic data access exceptions, simplifying calling code and making any exception that is thrown more meaningful.

Is aware of a respective connection bound to the current thread, for example when using DataSourceTransactionManager.

Parameters:
ds - DataSource to get connection from
Returns:
a JDBC connection from this DataSource
Throws:
CannotGetJdbcConnectionException - if we fail to get a connection from the given DataSource
See Also:
getThreadObjectManager(), DataSourceTransactionManager

closeConnectionIfNecessary

public static void closeConnectionIfNecessary(java.sql.Connection con,
                                              javax.sql.DataSource ds)
                                       throws CannotCloseJdbcConnectionException
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.
Parameters:
con - connection to close if necessary (if this is null, the call will be ignored)
ds - DataSource that the connection came from (can be null)

getCloseSuppressingConnectionProxy

public static java.sql.Connection getCloseSuppressingConnectionProxy(java.sql.Connection source)
Wrap the given connection with a proxy that delegates every method call to it but suppresses close calls. This is useful for allowing application code to handle a special framework connection just like an ordinary DataSource connection.
Parameters:
source - original connection
Returns:
the wrapped connection
See Also:
SingleConnectionDataSource


Rod Johnson and Spring contributors 2001-2003.