org.springframework.orm.ojb.support
Class PersistenceBrokerDaoSupport

java.lang.Object
  extended byorg.springframework.orm.ojb.support.PersistenceBrokerDaoSupport

public class PersistenceBrokerDaoSupport
extends Object

Convenient super class for OJB PersistenceBroker data access objects.

Allows a JDBC Connection Descriptor alias to be set, providing a PersistenceBrokerTemplate based on it to subclasses. Can alternatively be initialized directly via a PersistenceBrokerTemplate, to reuse the latter's settings like PBKey, DataSource, etc.

This base class is mainly intended for PersistenceBrokerTemplate usage but can also be used when working with OjbFactoryUtils directly. Convenience getPersistenceBroker and closePersistenceBrokerIfNecessary methods are provided for that usage style.

This class will create its own PersistenceBrokerTemplate if no explicit instance is passed in. The allowCreate flag on that PersistenceBrokerTemplate will be true by default. A custom PersistenceBrokerTemplate instance can be used through overriding createHibernateTemplate.

Since:
1.1
Author:
Juergen Hoeller
See Also:
setJcdAlias(java.lang.String), setPersistenceBrokerTemplate(org.springframework.orm.ojb.PersistenceBrokerTemplate), createPersistenceBrokerTemplate(), getPersistenceBroker(boolean), closePersistenceBrokerIfNecessary(org.apache.ojb.broker.PersistenceBroker), PersistenceBrokerTemplate

Field Summary
protected  Log logger
           
 
Constructor Summary
PersistenceBrokerDaoSupport()
           
 
Method Summary
 void afterPropertiesSet()
           
protected  void closePersistenceBrokerIfNecessary(org.apache.ojb.broker.PersistenceBroker pb)
          Close the given PersistenceBroker if it isn't bound to the thread.
protected  DataAccessException convertOjbAccessException(org.apache.ojb.broker.PersistenceBrokerException ex)
          Convert the given PersistenceBrokerException to an appropriate exception from the org.springframework.dao hierarchy.
protected  PersistenceBrokerTemplate createPersistenceBrokerTemplate()
          Create a new default PersistenceBrokerTemplate on initialization.
 String getJcdAlias()
          Return the JDBC Connection Descriptor alias of the PersistenceBroker configuration to use.
protected  org.apache.ojb.broker.PersistenceBroker getPersistenceBroker(boolean allowCreate)
          Get an OJB PersistenceBroker.
 PersistenceBrokerTemplate getPersistenceBrokerTemplate()
          Return the PersistenceBrokerTemplate for this DAO, pre-initialized with the JCD alias or set explicitly.
protected  void initDao()
          Subclasses can override this for custom initialization behavior.
 void setJcdAlias(String jcdAlias)
          Set the JDBC Connection Descriptor alias of the PersistenceBroker configuration to use.
 void setPersistenceBrokerTemplate(PersistenceBrokerTemplate persistenceBrokerTemplate)
          Set the PersistenceBrokerTemplate for this DAO explicitly, as an alternative to specifying a JCD alias.
 
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

PersistenceBrokerDaoSupport

public PersistenceBrokerDaoSupport()
Method Detail

createPersistenceBrokerTemplate

protected PersistenceBrokerTemplate createPersistenceBrokerTemplate()
Create a new default PersistenceBrokerTemplate on initialization. The returned template instance might be populated with a JCD alias, or get overwritten by an explicitly passed-in PersistenceBrokerTemplate.

Can be overridden in subclasses to provide a PersistenceBrokerTemplate instance with different configuration, or a custom PersistenceBrokerTemplate subclass.

Returns:
the new PersistenceBrokerTemplate instance
See Also:
setJcdAlias(java.lang.String), setPersistenceBrokerTemplate(org.springframework.orm.ojb.PersistenceBrokerTemplate)

setJcdAlias

public final void setJcdAlias(String jcdAlias)
Set the JDBC Connection Descriptor alias of the PersistenceBroker configuration to use. Default is the default connection configured for OJB.


getJcdAlias

public final String getJcdAlias()
Return the JDBC Connection Descriptor alias of the PersistenceBroker configuration to use.


setPersistenceBrokerTemplate

public final void setPersistenceBrokerTemplate(PersistenceBrokerTemplate persistenceBrokerTemplate)
Set the PersistenceBrokerTemplate for this DAO explicitly, as an alternative to specifying a JCD alias.


getPersistenceBrokerTemplate

public final PersistenceBrokerTemplate getPersistenceBrokerTemplate()
Return the PersistenceBrokerTemplate for this DAO, pre-initialized with the JCD alias or set explicitly.


afterPropertiesSet

public final void afterPropertiesSet()
                              throws Exception
Throws:
Exception

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

getPersistenceBroker

protected final org.apache.ojb.broker.PersistenceBroker getPersistenceBroker(boolean allowCreate)
                                                                      throws DataAccessResourceFailureException,
                                                                             IllegalStateException
Get an OJB PersistenceBroker. Is aware of a corresponding PersistenceBroker bound to the current thread, for example when using PersistenceBrokerTransactionManager. Will create a new PersistenceBroker else, if allowCreate is true.

Returns:
the PersistenceBroker
Throws:
DataAccessResourceFailureException - if the PersistenceBroker couldn't be created
IllegalStateException - if no thread-bound PersistenceBroker found and allowCreate false

convertOjbAccessException

protected final DataAccessException convertOjbAccessException(org.apache.ojb.broker.PersistenceBrokerException ex)
Convert the given PersistenceBrokerException to an appropriate exception from the org.springframework.dao hierarchy. In case of a wrapped SQLException, the PersistenceBrokerTemplate's SQLExceptionTranslator gets applied.

Parameters:
ex - PersistenceBrokerException that occured
Returns:
the corresponding DataAccessException instance
See Also:
OjbAccessor.convertOjbAccessException(org.apache.ojb.broker.PersistenceBrokerException)

closePersistenceBrokerIfNecessary

protected final void closePersistenceBrokerIfNecessary(org.apache.ojb.broker.PersistenceBroker pb)
Close the given PersistenceBroker if it isn't bound to the thread.

Parameters:
pb - PersistenceBroker to close


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