diff --git a/extensions/persist/src/com/google/inject/persist/jpa/JpaLocalTxnInterceptor.java b/extensions/persist/src/com/google/inject/persist/jpa/JpaLocalTxnInterceptor.java index 71d5b907e2..07d566db3f 100644 --- a/extensions/persist/src/com/google/inject/persist/jpa/JpaLocalTxnInterceptor.java +++ b/extensions/persist/src/com/google/inject/persist/jpa/JpaLocalTxnInterceptor.java @@ -65,6 +65,10 @@ public Object invoke(MethodInvocation methodInvocation) throws Throwable { try { result = methodInvocation.proceed(); + if (txn.isActive()) { + commitOrRollback(txn); + } + return result; } catch (Exception e) { // commit transaction only if rollback didnt occur if (rollbackIfNecessary(transactional, e, txn)) { @@ -74,33 +78,20 @@ public Object invoke(MethodInvocation methodInvocation) throws Throwable { // propagate whatever exception is thrown anyway throw e; } finally { - // Close the em if necessary (guarded so this code doesn't run unless catch fired). - if (null != didWeStartWork.get() && !txn.isActive()) { - didWeStartWork.remove(); - unitOfWork.end(); - } - } - - // everything was normal so commit the txn (do not move into try block above as it - // interferes with the advised method's throwing semantics) - try { - if (txn.isActive()) { - if (txn.getRollbackOnly()) { - txn.rollback(); - } else { - txn.commit(); - } - } - } finally { - // close the em if necessary + // Close the em if necessary if (null != didWeStartWork.get()) { didWeStartWork.remove(); unitOfWork.end(); } } + } - // or return result - return result; + private void commitOrRollback(EntityTransaction txn) { + if (txn.getRollbackOnly()) { + txn.rollback(); + } else { + txn.commit(); + } } // TODO(user): Cache this method's results.