org.springframework.extensions.jcr
Class TransactionAwareRepository

java.lang.Object
  extended by org.springframework.extensions.jcr.TransactionAwareRepository
All Implemented Interfaces:
org.springframework.beans.factory.FactoryBean, org.springframework.beans.factory.InitializingBean

public class TransactionAwareRepository
extends java.lang.Object
implements org.springframework.beans.factory.InitializingBean, org.springframework.beans.factory.FactoryBean

This FactoryBean exposes a proxy for a target JCR Repository, returning the current thread-bound Repository (the Spring-managed transactional Repository or the single OpenPersistenceManagerInView Repository) on login(), if any.

Essentially, login() calls get seamlessly forwarded to SessionFactoryUtils.getSession. Furthermore, Session.logout calls get forwarded to SessionFactoryUtils.releaseSession.

As the Session returned depends on the workspace and credentials given, this implementation accepts a JcrSessionFactory as parameter (basically a wrapper for Repository, Credentials and Workspace properties). The proxy will check the parameters and proxy the Repository for sessions that are retrieved with the credentials and workspace name defined on the session factory. Sessions retrieved with different workspace, credentials are not proxied.

The main advantage of this proxy is that it allows DAOs to work with a plain JCR Repository reference, while still participating in Spring's (or a J2EE server's) resource and transaction management. DAOs will only rely on the JCR API in such a scenario, without any Spring dependencies. DAOs could seamlessly switch between a JNDI Repository and this proxy for a local Repository 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 JCR-based DAOs with Spring Extensions's JcrTemplate, offering benefits such as consistent data access exceptions instead of RepositoryExceptions at the DAO layer. However, Spring's resource and transaction management (and Dependency Injection) will work for DAOs written against the plain JCR API too.

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

Author:
Costin Leau, Sergio Bossa, Salvatore Incandela

Constructor Summary
TransactionAwareRepository()
           
 
Method Summary
 void afterPropertiesSet()
           
 java.lang.Object getObject()
           
 java.lang.Class getObjectType()
           
 javax.jcr.Repository getTargetRepository()
          Return the target JCR Repository that this proxy delegates to.
 boolean isAllowCreate()
           
 boolean isAllowNonTxRepository()
           
 boolean isSingleton()
           
 void setAllowCreate(boolean allowCreate)
          Set whether the Repository proxy is allowed to create a non-transactional Session when no transactional Session can be found for the current thread.
 void setAllowNonTxRepository(boolean allowNonTxRepository)
          Set whether the Repository proxy is accepted even if it does not support transactions which allows the functionality of thread-bound session but without the tx support.
 void setTargetFactory(JcrSessionFactory target)
          Set the target JCR Repository that this proxy should delegate to wrapped in a JcrSessionFactory object along with the credentials and workspace.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TransactionAwareRepository

public TransactionAwareRepository()
Method Detail

getObject

public java.lang.Object getObject()
                           throws java.lang.Exception
Specified by:
getObject in interface org.springframework.beans.factory.FactoryBean
Throws:
java.lang.Exception
See Also:
FactoryBean.getObject()

getObjectType

public java.lang.Class getObjectType()
Specified by:
getObjectType in interface org.springframework.beans.factory.FactoryBean
See Also:
FactoryBean.getObjectType()

isSingleton

public boolean isSingleton()
Specified by:
isSingleton in interface org.springframework.beans.factory.FactoryBean
See Also:
FactoryBean.isSingleton()

isAllowCreate

public boolean isAllowCreate()
Returns:
Returns the allowCreate.

afterPropertiesSet

public void afterPropertiesSet()
                        throws java.lang.Exception
Specified by:
afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
Throws:
java.lang.Exception
See Also:
InitializingBean.afterPropertiesSet()

setAllowCreate

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

Default is "true". Can be turned off to enforce access to transactional Sessionss, which safely allows for DAOs written to get a Session without explicit closing (i.e. a Session.login() call without corresponding Session.logout() call).

Parameters:
allowCreate - The allowCreate to set.
See Also:
SessionFactoryUtils.getSession(SessionFactory, boolean)

isAllowNonTxRepository

public boolean isAllowNonTxRepository()
Returns:
Returns the allowNonTxRepository.

setAllowNonTxRepository

public void setAllowNonTxRepository(boolean allowNonTxRepository)
Set whether the Repository proxy is accepted even if it does not support transactions which allows the functionality of thread-bound session but without the tx support. Such an option exists because transaction support is optional for JSR-170 implementations.

Default is "true". Can be turned off to enforce only transactional Sessionss, which safely allows for DAOs written to get a Session without explicit closing (i.e. a Session.login() call without corresponding Session.logout() call).

Parameters:
allowNonTxRepository - The allowNonTxRepository to set.

setTargetFactory

public void setTargetFactory(JcrSessionFactory target)
Set the target JCR Repository that this proxy should delegate to wrapped in a JcrSessionFactory object along with the credentials and workspace.


getTargetRepository

public javax.jcr.Repository getTargetRepository()
Return the target JCR Repository that this proxy delegates to.



Copyright © 2009. All Rights Reserved.