From c03650620dc44ee4a122023fa9951b6a7b97b530 Mon Sep 17 00:00:00 2001 From: Erik Brakkee Date: Fri, 30 Apr 2010 14:19:35 +0000 Subject: [PATCH] jpauserset now working. --- .../persistence/TransactionProxyFactory.java | 31 ++++++++++++------- 1 file changed, 20 insertions(+), 11 deletions(-) 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 4e72f484..d3d2884b 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 @@ -20,6 +20,7 @@ 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; @@ -29,8 +30,8 @@ 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. * - * Use {@link #getTransactionScopedEntityManager()} to get the transaction scoped - * entity manager to pass to services. + * Use {@link #getTransactionScopedEntityManager()} to get the transaction + * scoped entity manager to pass to services. * * @param T * Type of interface to proxy. @@ -58,8 +59,15 @@ public class TransactionProxyFactory { ENTITY_MANAGER.set(aEm); return aMethod.invoke(service, aArgs); } catch (InvocationTargetException e) { - throw (Exception)e.getCause(); - } finally { + Throwable cause = e.getCause(); + if (cause instanceof Exception) { + throw (Exception) cause; + } else if (cause instanceof Error) { + throw (Error) cause; + } + // last resort. + throw new RuntimeException(e); + } finally { ENTITY_MANAGER.set(null); } } @@ -68,9 +76,9 @@ public class TransactionProxyFactory { } - private static final ThreadSpecificProxyFactory ENTITY_MANAGER = - new ThreadSpecificProxyFactory(EntityManager.class); - + private static final ThreadSpecificProxyFactory ENTITY_MANAGER = new ThreadSpecificProxyFactory( + EntityManager.class); + private JpaBuilder jpaBuilder; private Class clazz; @@ -83,8 +91,8 @@ public class TransactionProxyFactory { jpaBuilder = aJpaBuilder; clazz = aClass; } - - public EntityManager getTransactionScopedEntityManager() { + + public EntityManager getTransactionScopedEntityManager() { return ENTITY_MANAGER.getProxy(); } @@ -98,8 +106,9 @@ public class TransactionProxyFactory { new Class[] { InvocationHandler.class }).newInstance( new Object[] { handler }); return proxy; - } catch (Exception e) { - throw new RuntimeException("Could not create proxy for " + clazz.getName(), e); + } catch (Exception e) { + throw new RuntimeException("Could not create proxy for " + + clazz.getName(), e); } } } -- 2.31.1