From 640bc19800b63557320c465dc3794839a9eb7933 Mon Sep 17 00:00:00 2001 From: SantanuKar43 Date: Sat, 6 Sep 2025 09:33:14 +0530 Subject: [PATCH 1/2] refactor JpaLocalTxnInterceptor to always close the entitymanager --- .../persist/jpa/JpaLocalTxnInterceptor.java | 29 +++++-------------- 1 file changed, 7 insertions(+), 22 deletions(-) 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..2f081de1cc 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,12 @@ public Object invoke(MethodInvocation methodInvocation) throws Throwable { try { result = methodInvocation.proceed(); + if (txn.getRollbackOnly()) { + txn.rollback(); + } else { + txn.commit(); + } + return result; } catch (Exception e) { // commit transaction only if rollback didnt occur if (rollbackIfNecessary(transactional, e, txn)) { @@ -74,33 +80,12 @@ 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; } // TODO(user): Cache this method's results. From 597e4bd2c839f2ad66e3958b1a55926afa7578cd Mon Sep 17 00:00:00 2001 From: SantanuKar43 Date: Tue, 9 Sep 2025 09:54:24 +0530 Subject: [PATCH 2/2] add check for manually closed txns --- .../inject/persist/jpa/JpaLocalTxnInterceptor.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) 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 2f081de1cc..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,10 +65,8 @@ public Object invoke(MethodInvocation methodInvocation) throws Throwable { try { result = methodInvocation.proceed(); - if (txn.getRollbackOnly()) { - txn.rollback(); - } else { - txn.commit(); + if (txn.isActive()) { + commitOrRollback(txn); } return result; } catch (Exception e) { @@ -88,6 +86,14 @@ public Object invoke(MethodInvocation methodInvocation) throws Throwable { } } + private void commitOrRollback(EntityTransaction txn) { + if (txn.getRollbackOnly()) { + txn.rollback(); + } else { + txn.commit(); + } + } + // TODO(user): Cache this method's results. private Transactional readTransactionMetadata(MethodInvocation methodInvocation) { Transactional transactional;