From 1bf0e1d6acc1df4da77cfcf315bfe16f58a12def Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Mon, 17 Sep 2018 14:41:02 -0400 Subject: don't begin new transaction Persistence code was not checking to see if a transaction was already in progress before beginning a transaction. Fix checkstyle issue with comments and parameter for EntityMgrException. Restore other checkstyle fixes. More checkstyle fixes. Rename some variables. Change-Id: Ic820944781571ba2ef411cb86d12fa32fb206124 Issue-ID: POLICY-1106 Signed-off-by: Jim Hahn --- .../policy/drools/persistence/EntityMgrTrans.java | 62 +++++++++++++++------- 1 file changed, 44 insertions(+), 18 deletions(-) (limited to 'feature-session-persistence/src/main/java') 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 EntityManager that creates a JTA transaction that is auto-rolled back when - * closed. + * Wrapper for an EntityManager 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); } -- cgit 1.2.3-korg