spring-orm

org.springframework.orm.jdo
Class TransactionAwarePersistenceManagerFactoryProxy

java.lang.Object
  extended by org.springframework.orm.jdo.TransactionAwarePersistenceManagerFactoryProxy
All Implemented Interfaces:
org.springframework.beans.factory.FactoryBean<javax.jdo.PersistenceManagerFactory>

public class TransactionAwarePersistenceManagerFactoryProxy
extends java.lang.Object
implements org.springframework.beans.factory.FactoryBean<javax.jdo.PersistenceManagerFactory>

Proxy for a target JDO PersistenceManagerFactory, returning the current thread-bound PersistenceManager (the Spring-managed transactional PersistenceManager or the single OpenPersistenceManagerInView PersistenceManager) on getPersistenceManager(), if any.

Essentially, getPersistenceManager() calls get seamlessly forwarded to PersistenceManagerFactoryUtils.getPersistenceManager(javax.jdo.PersistenceManagerFactory, boolean). Furthermore, PersistenceManager.close calls get forwarded to PersistenceManagerFactoryUtils.releasePersistenceManager(javax.jdo.PersistenceManager, javax.jdo.PersistenceManagerFactory).

The main advantage of this proxy is that it allows DAOs to work with a plain JDO PersistenceManagerFactory reference, while still participating in Spring's (or a J2EE server's) resource and transaction management. DAOs will only rely on the JDO API in such a scenario, without any Spring dependencies.

Note that the behavior of this proxy matches the behavior that the JDO spec defines for a PersistenceManagerFactory as exposed by a JCA connector, when deployed in a J2EE server. Hence, DAOs could seamlessly switch between a JNDI PersistenceManagerFactory and this proxy for a local PersistenceManagerFactory, receiving the reference through Dependency Injection. This will work without any Spring API dependencies in the DAO code!

It is usually preferable to write your JDO-based DAOs with Spring's JdoTemplate, offering benefits such as consistent data access exceptions instead of JDOExceptions at the DAO layer. However, Spring's resource and transaction management (and Dependency Injection) will work for DAOs written against the plain JDO API as well.

Of course, you can still access the target PersistenceManagerFactory even when your DAOs go through this proxy, by defining a bean reference that points directly at your target PersistenceManagerFactory bean.

Since:
1.2
Author:
Juergen Hoeller
See Also:
PersistenceManagerFactory.getPersistenceManager(), PersistenceManager.close(), PersistenceManagerFactoryUtils.getPersistenceManager(javax.jdo.PersistenceManagerFactory, boolean), PersistenceManagerFactoryUtils.releasePersistenceManager(javax.jdo.PersistenceManager, javax.jdo.PersistenceManagerFactory)

Constructor Summary
TransactionAwarePersistenceManagerFactoryProxy()
           
 
Method Summary
 javax.jdo.PersistenceManagerFactory getObject()
           
 java.lang.Class<? extends javax.jdo.PersistenceManagerFactory> getObjectType()
           
 javax.jdo.PersistenceManagerFactory getTargetPersistenceManagerFactory()
          Return the target JDO PersistenceManagerFactory that this proxy delegates to.
protected  boolean isAllowCreate()
          Return whether the PersistenceManagerFactory proxy is allowed to create a non-transactional PersistenceManager when no transactional PersistenceManager can be found for the current thread.
 boolean isSingleton()
           
 void setAllowCreate(boolean allowCreate)
          Set whether the PersistenceManagerFactory proxy is allowed to create a non-transactional PersistenceManager when no transactional PersistenceManager can be found for the current thread.
 void setTargetPersistenceManagerFactory(javax.jdo.PersistenceManagerFactory target)
          Set the target JDO PersistenceManagerFactory that this proxy should delegate to.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TransactionAwarePersistenceManagerFactoryProxy

public TransactionAwarePersistenceManagerFactoryProxy()
Method Detail

setTargetPersistenceManagerFactory

public void setTargetPersistenceManagerFactory(javax.jdo.PersistenceManagerFactory target)
Set the target JDO PersistenceManagerFactory that this proxy should delegate to. This should be the raw PersistenceManagerFactory, as accessed by JdoTransactionManager.

See Also:
JdoTransactionManager

getTargetPersistenceManagerFactory

public javax.jdo.PersistenceManagerFactory getTargetPersistenceManagerFactory()
Return the target JDO PersistenceManagerFactory that this proxy delegates to.


setAllowCreate

public void setAllowCreate(boolean allowCreate)
Set whether the PersistenceManagerFactory proxy is allowed to create a non-transactional PersistenceManager when no transactional PersistenceManager can be found for the current thread.

Default is "true". Can be turned off to enforce access to transactional PersistenceManagers, which safely allows for DAOs written to get a PersistenceManager without explicit closing (i.e. a PersistenceManagerFactory.getPersistenceManager() call without corresponding PersistenceManager.close() call).

See Also:
PersistenceManagerFactoryUtils.getPersistenceManager(javax.jdo.PersistenceManagerFactory, boolean)

isAllowCreate

protected boolean isAllowCreate()
Return whether the PersistenceManagerFactory proxy is allowed to create a non-transactional PersistenceManager when no transactional PersistenceManager can be found for the current thread.


getObject

public javax.jdo.PersistenceManagerFactory getObject()
Specified by:
getObject in interface org.springframework.beans.factory.FactoryBean<javax.jdo.PersistenceManagerFactory>

getObjectType

public java.lang.Class<? extends javax.jdo.PersistenceManagerFactory> getObjectType()
Specified by:
getObjectType in interface org.springframework.beans.factory.FactoryBean<javax.jdo.PersistenceManagerFactory>

isSingleton

public boolean isSingleton()
Specified by:
isSingleton in interface org.springframework.beans.factory.FactoryBean<javax.jdo.PersistenceManagerFactory>

spring-orm