org.springframework.jdbc.core.support
Class JdbcDaoSupport

java.lang.Object
  extended byorg.springframework.jdbc.core.support.JdbcDaoSupport
All Implemented Interfaces:
InitializingBean

public abstract class JdbcDaoSupport
extends Object
implements InitializingBean

Convenient super class for JDBC data access objects. Requires a DataSource to be set, providing a JdbcTemplate based on it to subclasses.

This base class is mainly intended for JdbcTemplate usage but can also be used when working with DataSourceUtils directly or with org.springframework.jdbc.object classes.

Since:
28.07.2003
Author:
Juergen Hoeller
See Also:
setDataSource(javax.sql.DataSource), JdbcTemplate, DataSourceUtils

Field Summary
protected  Log logger
           
 
Constructor Summary
JdbcDaoSupport()
           
 
Method Summary
 void afterPropertiesSet()
          Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).
protected  void closeConnectionIfNecessary(Connection con)
          Close the given JDBC Connection if necessary, created via this bean's DataSource, if it isn't bound to the thread.
protected  Connection getConnection()
          Get a JDBC Connection, either from the current transaction or a new one.
 DataSource getDataSource()
          Return the JDBC DataSource used by this DAO.
protected  SQLExceptionTranslator getExceptionTranslator()
          Return the SQLExceptionTranslator of this DAO's JdbcTemplate, for translating SQLExceptions in custom JDBC access code.
 JdbcTemplate getJdbcTemplate()
          Return the JdbcTemplate for this DAO, pre-initialized with the DataSource or set explicitly.
protected  void initDao()
          Subclasses can override this for custom initialization behavior.
 void setDataSource(DataSource dataSource)
          Set the JDBC DataSource to be used by this DAO.
 void setJdbcTemplate(JdbcTemplate jdbcTemplate)
          Set the JdbcTemplate for this DAO explicitly, as an alternative to specifying a DataSource.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected final Log logger
Constructor Detail

JdbcDaoSupport

public JdbcDaoSupport()
Method Detail

setDataSource

public final void setDataSource(DataSource dataSource)
Set the JDBC DataSource to be used by this DAO.


getDataSource

public final DataSource getDataSource()
Return the JDBC DataSource used by this DAO.


setJdbcTemplate

public final void setJdbcTemplate(JdbcTemplate jdbcTemplate)
Set the JdbcTemplate for this DAO explicitly, as an alternative to specifying a DataSource.


getJdbcTemplate

public final JdbcTemplate getJdbcTemplate()
Return the JdbcTemplate for this DAO, pre-initialized with the DataSource or set explicitly.


afterPropertiesSet

public final void afterPropertiesSet()
                              throws Exception
Description copied from interface: InitializingBean
Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).

This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.

Specified by:
afterPropertiesSet in interface InitializingBean
Throws:
Exception - in the event of misconfiguration (such as failure to set an essential property) or if initialization fails.

initDao

protected void initDao()
                throws Exception
Subclasses can override this for custom initialization behavior. Gets called after population of this instance's bean properties.

Throws:
Exception - if initialization fails

getConnection

protected final Connection getConnection()
                                  throws CannotGetJdbcConnectionException
Get a JDBC Connection, either from the current transaction or a new one.

Returns:
the JDBC Connection
Throws:
CannotGetJdbcConnectionException - if the attempt to get a Connection failed

getExceptionTranslator

protected final SQLExceptionTranslator getExceptionTranslator()
Return the SQLExceptionTranslator of this DAO's JdbcTemplate, for translating SQLExceptions in custom JDBC access code.


closeConnectionIfNecessary

protected final void closeConnectionIfNecessary(Connection con)
Close the given JDBC Connection if necessary, created via this bean's DataSource, if it isn't bound to the thread.

Parameters:
con - Connection to close


Copyright (C) 2003-2004 The Spring Framework Project.