aboutsummaryrefslogtreecommitdiffstats
path: root/feature-session-persistence/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'feature-session-persistence/src/main/java')
-rw-r--r--feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/EntityMgrTrans.java62
1 files changed, 44 insertions, 18 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 3d276519..988cd9c2 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
@@ -28,27 +28,40 @@ 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 JTA transaction that is auto-rolled back when
- * closed.
+ * Wrapper for an <i>EntityManager</i> that creates a JTA transaction that is auto-rolled
+ * back when closed. Note: commit() and rollback() actions do nothing if a transaction was
+ * already in progress when this object was constructed.
*/
public class EntityMgrTrans extends EntityMgrCloser {
- /** Transaction to be rolled back. */
+ /**
+ * Transaction to be rolled back.
+ */
private static UserTransaction userTrans = com.arjuna.ats.jta.UserTransaction.userTransaction();
/**
+ * {@code True} if a transaction had already been started before this object was
+ * constructed, {@code false} otherwise.
+ */
+ private boolean transInProgress;
+
+ /**
* Constructor.
- *
- * @param em entity for which a transaction is to be begun */
+ *
+ * @param em entity for which a transaction is to be begun
+ */
public EntityMgrTrans(EntityManager em) {
super(em);
try {
- userTrans.begin();
+ transInProgress = (userTrans.getStatus() == Status.STATUS_ACTIVE);
+ if (!transInProgress) {
+ userTrans.begin();
+ }
+
em.joinTransaction();
} catch (RuntimeException | NotSupportedException | SystemException e) {
@@ -75,24 +88,32 @@ public class EntityMgrTrans extends EntityMgrCloser {
EntityMgrTrans.userTrans = userTrans;
}
- /** Commits the transaction. */
+ /**
+ * Commits the transaction.
+ */
public void commit() {
+ if (transInProgress) {
+ return;
+ }
+
try {
userTrans.commit();
- } catch (SecurityException
- | IllegalStateException
- | RollbackException
- | HeuristicMixedException
- | HeuristicRollbackException
- | SystemException e) {
+ } catch (SecurityException | IllegalStateException | RollbackException | HeuristicMixedException
+ | HeuristicRollbackException | SystemException e) {
throw new EntityMgrException(e);
}
}
- /** Rolls back the transaction. */
+ /**
+ * Rolls back the transaction.
+ */
public void rollback() {
+ if (transInProgress) {
+ return;
+ }
+
try {
userTrans.rollback();
@@ -104,7 +125,7 @@ public class EntityMgrTrans extends EntityMgrCloser {
@Override
public void close() {
try {
- if (userTrans.getStatus() == Status.STATUS_ACTIVE) {
+ if (!transInProgress && userTrans.getStatus() == Status.STATUS_ACTIVE) {
userTrans.rollback();
}
@@ -117,12 +138,17 @@ public class EntityMgrTrans extends EntityMgrCloser {
}
/**
- * Runtime exceptions generated by this class. Wraps exceptions generated by delegated operations,
- * particularly when they are not, themselves, Runtime exceptions.
+ * 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;
+ /**
+ * Constructor.
+ *
+ * @param ex exception to be wrapped
+ */
public EntityMgrException(Exception ex) {
super(ex);
}