The Spring Framework

org.springframework.orm.jdo
Class LocalPersistenceManagerFactoryBean

java.lang.Object
  extended by org.springframework.orm.jdo.LocalPersistenceManagerFactoryBean
All Implemented Interfaces:
DisposableBean, FactoryBean, InitializingBean, PersistenceExceptionTranslator

public class LocalPersistenceManagerFactoryBean
extends Object
implements FactoryBean, InitializingBean, DisposableBean, PersistenceExceptionTranslator

FactoryBean that creates a JDO PersistenceManagerFactory. This is the usual way to set up a shared JDO PersistenceManagerFactory in a Spring application context; the PersistenceManagerFactory can then be passed to JDO-based DAOs via dependency injection. Note that switching to a JNDI lookup or to a bean-style PersistenceManagerFactory instance is just a matter of configuration!

Configuration settings can either be read from a properties file, specified as "configLocation", or completely via this class. Properties specified as "jdoProperties" here will override any settings in a file.

This PersistenceManager handling strategy is most appropriate for applications which solely use JDO for data access. In this case, JdoTransactionManager is more convenient than setting up your JDO provider for JTA transactions (which might involve a JCA connector).

NOTE: This class is compatible with both JDO 1.0 and JDO 2.0, as far as possible. It uses reflection to adapt to the actual API present on the class path (concretely: for the getPersistenceManagerFactory method with either a Properties or a Map argument). Make sure that the JDO API jar on your class path matches the one that your JDO provider has been compiled against!

This class also implements the PersistenceExceptionTranslator interface, as autodetected by Spring's PersistenceExceptionTranslationPostProcessor, for AOP-based translation of native exceptions to Spring DataAccessExceptions. Hence, the presence of a LocalPersistenceManagerFactoryBean automatically enables a PersistenceExceptionTranslationPostProcessor to translate JDO exceptions.

Alternative: Configuration of a PersistenceManagerFactory provider bean

As alternative to the properties-driven approach that this FactoryBean offers (which is analogous to using the standard JDOHelper class with a Properties object that is populated with standard JDO properties), you can set up an instance of your PersistenceManagerFactory implementation class directly.

Like a DataSource, a PersistenceManagerFactory is encouraged to support bean-style configuration, which makes it very easy to set up as Spring-managed bean. The implementation class becomes the bean class; the remaining properties are applied as bean properties (starting with lower-case characters, in contrast to the corresponding JDO properties).

For example, in case of JPOX:

 <bean id="persistenceManagerFactory" class="org.jpox.PersistenceManagerFactoryImpl" destroy-method="close">
   <property name="connectionFactory" ref="dataSource"/>
   <property name="nontransactionalRead" value="true"/>
 </bean>
 

Note that such direct setup of a PersistenceManagerFactory implementation is the only way to pass an external connection factory (i.e. a JDBC DataSource) into a JDO PersistenceManagerFactory. With the standard properties-driven approach, you can only use an internal connection pool or a JNDI DataSource.

The close() method is standardized as of JDO 1.0.1; don't forget to specify it as "destroy-method" for any PersistenceManagerFactory instance. Note that this FactoryBean will automatically invoke close() for the PersistenceManagerFactory that it creates, without any special configuration.

Since:
03.06.2003
Author:
Juergen Hoeller
See Also:
JdoAccessor.setPersistenceManagerFactory(javax.jdo.PersistenceManagerFactory), JdoTransactionManager.setPersistenceManagerFactory(javax.jdo.PersistenceManagerFactory), JndiObjectFactoryBean, JDOHelper.getPersistenceManagerFactory(java.util.Map), PersistenceManagerFactory.setConnectionFactory(java.lang.Object), PersistenceManagerFactory.close(), PersistenceExceptionTranslationPostProcessor

Field Summary
protected  Log logger
           
 
Constructor Summary
LocalPersistenceManagerFactoryBean()
           
 
Method Summary
 void afterPropertiesSet()
          Initialize the PersistenceManagerFactory for the given location.
 void destroy()
          Close the PersistenceManagerFactory on bean factory shutdown.
 Properties getJdoProperties()
          Return the JDO properties, if any.
 Object getObject()
          Return the singleton PersistenceManagerFactory.
 Class getObjectType()
          Return the type of object that this FactoryBean creates, or null if not known in advance.
 boolean isSingleton()
          Is the object managed by this factory a singleton?
protected  javax.jdo.PersistenceManagerFactory newPersistenceManagerFactory(Properties props)
          Subclasses can override this to perform custom initialization of the PersistenceManagerFactory instance, creating it via the given Properties that got prepared by this LocalPersistenceManagerFactoryBean.
 void setConfigLocation(Resource configLocation)
          Set the location of the JDO properties config file, for example as classpath resource "classpath:kodo.properties".
 void setJdoDialect(JdoDialect jdoDialect)
          Set the JDO dialect to use for the PersistenceExceptionTranslator functionality of this factory.
 void setJdoProperties(Properties jdoProperties)
          Set JDO properties, such as"javax.jdo.PersistenceManagerFactoryClass".
 DataAccessException translateExceptionIfPossible(RuntimeException ex)
          Implementation of the PersistenceExceptionTranslator interface, as autodetected by Spring's PersistenceExceptionTranslationPostProcessor.
 
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

LocalPersistenceManagerFactoryBean

public LocalPersistenceManagerFactoryBean()
Method Detail

setConfigLocation

public void setConfigLocation(Resource configLocation)
Set the location of the JDO properties config file, for example as classpath resource "classpath:kodo.properties".

Note: Can be omitted when all necessary properties are specified locally via this bean.


setJdoProperties

public void setJdoProperties(Properties jdoProperties)
Set JDO properties, such as"javax.jdo.PersistenceManagerFactoryClass".

Can be used to override values in a JDO properties config file, or to specify all necessary properties locally.


getJdoProperties

public Properties getJdoProperties()
Return the JDO properties, if any. Mainly available for configuration through property paths that specify individual keys.


setJdoDialect

public void setJdoDialect(JdoDialect jdoDialect)
Set the JDO dialect to use for the PersistenceExceptionTranslator functionality of this factory.

Default is a DefaultJdoDialect based on the PersistenceManagerFactory's underlying DataSource, if any.

See Also:
JdoDialect.translateException(javax.jdo.JDOException), translateExceptionIfPossible(java.lang.RuntimeException), PersistenceExceptionTranslator

afterPropertiesSet

public void afterPropertiesSet()
                        throws IllegalArgumentException,
                               IOException,
                               javax.jdo.JDOException
Initialize the PersistenceManagerFactory for the given location.

Specified by:
afterPropertiesSet in interface InitializingBean
Throws:
IllegalArgumentException - in case of illegal property values
IOException - if the properties could not be loaded from the given location
javax.jdo.JDOException - in case of JDO initialization errors

newPersistenceManagerFactory

protected javax.jdo.PersistenceManagerFactory newPersistenceManagerFactory(Properties props)
Subclasses can override this to perform custom initialization of the PersistenceManagerFactory instance, creating it via the given Properties that got prepared by this LocalPersistenceManagerFactoryBean.

The default implementation invokes JDOHelper's getPersistenceManagerFactory method. A custom implementation could prepare the instance in a specific way, or use a custom PersistenceManagerFactory implementation.

Implemented to work with either the JDO 1.0 getPersistenceManagerFactory(java.util.Properties) method or the JDO 2.0 getPersistenceManagerFactory(java.util.Map) method, detected through reflection.

Parameters:
props - the merged Properties prepared by this LocalPersistenceManagerFactoryBean
Returns:
the PersistenceManagerFactory instance
See Also:
JDOHelper.getPersistenceManagerFactory(java.util.Map)

getObject

public Object getObject()
Return the singleton PersistenceManagerFactory.

Specified by:
getObject in interface FactoryBean
Returns:
an instance of the bean (can be null)
See Also:
FactoryBeanNotInitializedException

getObjectType

public Class getObjectType()
Description copied from interface: FactoryBean
Return the type of object that this FactoryBean creates, or null if not known in advance.

This allows one to check for specific types of beans without instantiating objects, for example on autowiring.

In the case of implementations that are creating a singleton object, this method should try to avoid singleton creation as far as possible; it should rather estimate the type in advance. For prototypes, returning a meaningful type here is advisable too.

This method can be called before this FactoryBean has been fully initialized. It must not rely on state created during initialization; of course, it can still use such state if available.

NOTE: Autowiring will simply ignore FactoryBeans that return null here. Therefore it is highly recommended to implement this method properly, using the current state of the FactoryBean.

Specified by:
getObjectType in interface FactoryBean
Returns:
the type of object that this FactoryBean creates, or null if not known at the time of the call
See Also:
ListableBeanFactory.getBeansOfType(java.lang.Class)

isSingleton

public boolean isSingleton()
Description copied from interface: FactoryBean
Is the object managed by this factory a singleton? That is, will FactoryBean.getObject() always return the same object (a reference that can be cached)?

NOTE: If a FactoryBean indicates to hold a singleton object, the object returned from getObject() might get cached by the owning BeanFactory. Hence, do not return true unless the FactoryBean always exposes the same reference.

The singleton status of the FactoryBean itself will generally be provided by the owning BeanFactory; usually, it has to be defined as singleton there.

NOTE: This method returning false does not necessarily indicate that returned objects are independent instances. An implementation of the extended SmartFactoryBean interface may explicitly indicate independent instances through its SmartFactoryBean.isPrototype() method. Plain FactoryBean implementations which do not implement this extended interface are simply assumed to always return independent instances if the isSingleton() implementation returns false.

Specified by:
isSingleton in interface FactoryBean
Returns:
if the exposed object is a singleton
See Also:
FactoryBean.getObject(), SmartFactoryBean.isPrototype()

translateExceptionIfPossible

public DataAccessException translateExceptionIfPossible(RuntimeException ex)
Implementation of the PersistenceExceptionTranslator interface, as autodetected by Spring's PersistenceExceptionTranslationPostProcessor.

Converts the exception if it is a JDOException, preferably using a specified JdoDialect. Else returns null to indicate an unknown exception.

Specified by:
translateExceptionIfPossible in interface PersistenceExceptionTranslator
Parameters:
ex - a RuntimeException thrown
Returns:
the corresponding DataAccessException (or null if the exception could not be translated, as in this case it may result from user code rather than an actual persistence problem)
See Also:
PersistenceExceptionTranslationPostProcessor, JdoDialect.translateException(javax.jdo.JDOException), PersistenceManagerFactoryUtils.convertJdoAccessException(javax.jdo.JDOException)

destroy

public void destroy()
Close the PersistenceManagerFactory on bean factory shutdown.

Specified by:
destroy in interface DisposableBean

The Spring Framework

Copyright © 2002-2007 The Spring Framework.