summaryrefslogtreecommitdiffstats
path: root/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/EntityMgrTrans.java
diff options
context:
space:
mode:
Diffstat (limited to 'feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/EntityMgrTrans.java')
-rw-r--r--feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/EntityMgrTrans.java83
1 files changed, 73 insertions, 10 deletions
diff --git a/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/EntityMgrTrans.java b/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/EntityMgrTrans.java
index 79b620d3..9bb26ac1 100644
--- a/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/EntityMgrTrans.java
+++ b/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/EntityMgrTrans.java
@@ -21,10 +21,18 @@
package org.onap.policy.drools.persistence;
import javax.persistence.EntityManager;
-import javax.persistence.EntityTransaction;
+import javax.transaction.HeuristicMixedException;
+import javax.transaction.HeuristicRollbackException;
+import javax.transaction.NotSupportedException;
+import javax.transaction.RollbackException;
+import javax.transaction.Status;
+import javax.transaction.SystemException;
+import javax.transaction.UserTransaction;
+
+import org.onap.policy.common.utils.jpa.EntityMgrCloser;
/**
- * Wrapper for an <i>EntityManager</i> that creates a transaction that is
+ * Wrapper for an <i>EntityManager</i> that creates a JTA transaction that is
* auto-rolled back when closed.
*/
public class EntityMgrTrans extends EntityMgrCloser {
@@ -32,7 +40,7 @@ public class EntityMgrTrans extends EntityMgrCloser {
/**
* Transaction to be rolled back.
*/
- private EntityTransaction trans;
+ private static UserTransaction userTrans = com.arjuna.ats.jta.UserTransaction.userTransaction();
/**
*
@@ -43,39 +51,94 @@ public class EntityMgrTrans extends EntityMgrCloser {
super(em);
try {
- trans = em.getTransaction();
- trans.begin();
+ userTrans.begin();
+ em.joinTransaction();
} catch (RuntimeException e) {
em.close();
- throw e;
+ throw new EntityMgrException(e);
+
+ } catch (NotSupportedException | SystemException e) {
+ em.close();
+ throw new EntityMgrException(e);
}
}
/**
+ * Gets the user transaction. For use by junit tests.
+ *
+ * @return the user transaction
+ */
+ protected static UserTransaction getUserTrans() {
+ return userTrans;
+ }
+
+ /**
+ * Sets the user transaction. For use by junit tests.
+ *
+ * @param userTrans
+ * the new user transaction
+ */
+ protected static void setUserTrans(UserTransaction userTrans) {
+ EntityMgrTrans.userTrans = userTrans;
+ }
+
+ /**
* Commits the transaction.
*/
public void commit() {
- trans.commit();
+ try {
+ userTrans.commit();
+
+ } catch (SecurityException | IllegalStateException | RollbackException | HeuristicMixedException
+ | HeuristicRollbackException | SystemException e) {
+
+ throw new EntityMgrException(e);
+ }
}
/**
* Rolls back the transaction.
*/
public void rollback() {
- trans.rollback();
+ try {
+ userTrans.rollback();
+
+ } catch (IllegalStateException | SecurityException | SystemException e) {
+ throw new EntityMgrException(e);
+ }
}
@Override
public void close() {
try {
- if (trans.isActive()) {
- trans.rollback();
+ if (userTrans.getStatus() == Status.STATUS_ACTIVE) {
+ userTrans.rollback();
}
+ } catch (IllegalStateException | SecurityException | SystemException e) {
+ throw new EntityMgrException(e);
+
} finally {
super.close();
}
}
+ /**
+ * Runtime exceptions generated by this class. Wraps exceptions generated by
+ * delegated operations, particularly when they are not, themselves, Runtime
+ * exceptions.
+ */
+ public static class EntityMgrException extends RuntimeException {
+ private static final long serialVersionUID = 1L;
+
+ /**
+ *
+ * @param e
+ * exception to be wrapped
+ */
+ public EntityMgrException(Exception e) {
+ super(e);
+ }
+ }
}