import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
+import javax.management.RuntimeErrorException;
import javax.persistence.EntityManager;
import org.wamblee.support.ThreadSpecificProxyFactory;
* 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.
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);
}
}
}
- private static final ThreadSpecificProxyFactory<EntityManager> ENTITY_MANAGER =
- new ThreadSpecificProxyFactory<EntityManager>(EntityManager.class);
-
+ private static final ThreadSpecificProxyFactory<EntityManager> ENTITY_MANAGER = new ThreadSpecificProxyFactory<EntityManager>(
+ EntityManager.class);
+
private JpaBuilder jpaBuilder;
private Class<T> clazz;
jpaBuilder = aJpaBuilder;
clazz = aClass;
}
-
- public EntityManager getTransactionScopedEntityManager() {
+
+ public EntityManager getTransactionScopedEntityManager() {
return ENTITY_MANAGER.getProxy();
}
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);
}
}
}