diff options
author | Jim Hahn <jrh3@att.com> | 2018-09-17 14:41:02 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2018-09-17 16:11:24 -0400 |
commit | 1bf0e1d6acc1df4da77cfcf315bfe16f58a12def (patch) | |
tree | 82f77afe2e7d1af95a12b018e7a8a67e48d5add8 /feature-session-persistence/src/main/java | |
parent | 02641b81ea49d3ecc7ab102d17b921a1ed164bd3 (diff) |
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 <jrh3@att.com>
Diffstat (limited to 'feature-session-persistence/src/main/java')
-rw-r--r-- | feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/EntityMgrTrans.java | 62 |
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); } |