X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=test%2Fenterprise%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fsupport%2Fpersistence%2FTransactionProxyFactory.java;h=548a3cdaebc1a8279ebaef779854807387925a99;hb=dec278a67997ea8e85d10662e31548afd8890ed3;hp=d3d2884b79b5db87bcba6475455f9225d4a8bc3f;hpb=c03650620dc44ee4a122023fa9951b6a7b97b530;p=utils diff --git a/test/enterprise/src/main/java/org/wamblee/support/persistence/TransactionProxyFactory.java b/test/enterprise/src/main/java/org/wamblee/support/persistence/TransactionProxyFactory.java index d3d2884b..548a3cda 100644 --- a/test/enterprise/src/main/java/org/wamblee/support/persistence/TransactionProxyFactory.java +++ b/test/enterprise/src/main/java/org/wamblee/support/persistence/TransactionProxyFactory.java @@ -15,12 +15,12 @@ */ package org.wamblee.support.persistence; + import java.lang.reflect.InvocationHandler; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Proxy; -import javax.management.RuntimeErrorException; import javax.persistence.EntityManager; import org.wamblee.support.ThreadSpecificProxyFactory; @@ -28,11 +28,25 @@ import org.wamblee.support.persistence.JpaBuilder.JpaUnitOfWork; /** * This utility makes sure that each invocation on a certain interface is - * carried out within a JPA unit of work. + * carried out within a JPA unit of work. Note that this is equivalent + * to the sementics of a requiresNew transaction attribute. * * Use {@link #getTransactionScopedEntityManager()} to get the transaction * scoped entity manager to pass to services. * + * + * For example: + *
+ *     JpaBuilder builder = ...
+ *     TransactionProxyFactory factory = new TransactionProxyFactory(
+ *           builder, Service.class);
+ *     Service service = new JpaService(factory.getTransactionScopedEntityManager());
+ *     Service proxy = factory.getProxy(service);
+ *     proxy.executeMethod(...); 
+ * 
+ * The above example executes the executeMethod() call on the service object within an active transaction. + * In the constructor of the service a transaction scoped entity manager is passed. + * * @param T * Type of interface to proxy. * @@ -40,6 +54,13 @@ import org.wamblee.support.persistence.JpaBuilder.JpaUnitOfWork; */ public class TransactionProxyFactory { + /** + * Executes the call on the service within a new transaction. + * + * @author Erik Brakkee + * + * @param Type of the service interface. + */ private class UnitOfWorkInvocationHandler implements InvocationHandler { private T service; @@ -55,6 +76,7 @@ public class TransactionProxyFactory { .execute(new JpaUnitOfWork() { @Override public Object execute(EntityManager aEm) throws Exception { + EntityManager oldEm = ENTITY_MANAGER.get(); try { ENTITY_MANAGER.set(aEm); return aMethod.invoke(service, aArgs); @@ -68,7 +90,7 @@ public class TransactionProxyFactory { // last resort. throw new RuntimeException(e); } finally { - ENTITY_MANAGER.set(null); + ENTITY_MANAGER.set(oldEm); } } });