sk.baka.tools.jpa
Class SimpleTransactionInterceptor

java.lang.Object
  extended by sk.baka.tools.jpa.SimpleTransactionInterceptor
All Implemented Interfaces:
org.aopalliance.aop.Advice, org.aopalliance.intercept.Interceptor, org.aopalliance.intercept.MethodInterceptor

public class SimpleTransactionInterceptor
extends Object
implements org.aopalliance.intercept.MethodInterceptor

Intercepts the transactional invocations and starts/commits/rollbacks transaction as necessary. To use this interceptor you have to configure your DI framework to intercept methods running in transaction with this interceptor. You can for example mark such methods with an annotation or the like.

Author:
Martin Vysny

Constructor Summary
SimpleTransactionInterceptor(javax.persistence.EntityManagerFactory factory)
          Uses given factory to create entity managers.
 
Method Summary
 javax.persistence.EntityManager getEntityManager()
          Creates an EntityManager which delegates calls to a real entity manager stored in the tran thread storage.
 Object invoke(org.aopalliance.intercept.MethodInvocation invocation)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SimpleTransactionInterceptor

public SimpleTransactionInterceptor(javax.persistence.EntityManagerFactory factory)
Uses given factory to create entity managers.

Parameters:
factory - the factory to use.
Method Detail

getEntityManager

public javax.persistence.EntityManager getEntityManager()
Creates an EntityManager which delegates calls to a real entity manager stored in the tran thread storage. If no such entity manager is available then we are not in a transaction and IllegalStateException is thrown. You can configure your DI framework to inject returned instance to objects - it will work even if the transaction is not yet started.

We have to provide an EntityManager instance instead of a provider because the member injection happens before actual method execution, therefore the method is not yet intercepted and the transaction is not yet started.

Returns:
never null entity manager.

invoke

public Object invoke(org.aopalliance.intercept.MethodInvocation invocation)
              throws Throwable
Specified by:
invoke in interface org.aopalliance.intercept.MethodInterceptor
Throws:
Throwable


Copyright © 2009-2010. All Rights Reserved.