Skip to content

Commit

Permalink
Mark transaction as inactive in JtaTransactionManager during AfterCom…
Browse files Browse the repository at this point in the history
…pletion to invalidate transaction ThreadLocal
  • Loading branch information
VictorJimenezKwast committed Jun 18, 2024
1 parent 925ceaf commit a7dafa0
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 3 deletions.
2 changes: 2 additions & 0 deletions ebean-api/src/main/java/io/ebean/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ static Transaction current() {
*/
boolean isActive();

void setActive(boolean active);

/**
* Set the behavior for document store updates on this transaction.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ public boolean isActive() {
return transaction.isActive();
}

@Override
public void setActive(boolean active) {
transaction.setActive(active);
}

@Override
public void setPersistCascade(boolean persistCascade) {
transaction.setPersistCascade(persistCascade);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,11 @@ public boolean isActive() {
return active;
}

@Override
public void setActive(boolean active) {
this.active = active;
}

@Override
public boolean isPersistCascade() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,11 @@ public boolean isActive() {
return active;
}

@Override
public void setActive(boolean active) {
this.active = active;
}

@Override
public final boolean isPersistCascade() {
return persistCascade;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,16 @@ public Object getCurrentTransaction() {
// check current Ebean transaction
SpiTransaction currentEbeanTransaction = scope.inScope();
if (currentEbeanTransaction != null) {
// NOT expecting this so log WARNING
log.log(WARNING, "JTA Transaction - no current txn BUT using current Ebean one {0}", currentEbeanTransaction.id());
return currentEbeanTransaction;
if (currentEbeanTransaction.isActive()) {
// NOT expecting this so log WARNING
log.log(WARNING, "JTA Transaction - no current txn BUT using current Ebean one {0}", currentEbeanTransaction.id());
return currentEbeanTransaction;
} else {
log.log(WARNING, "JTA Transaction - no current txn, but found Ebean inactive transaction. Clearing.", currentEbeanTransaction.id());
// thread local references no longer active transaction. This can happen if the JtaTxnListener#afterCompletion
// was triggered by a different thread than the one which created the scope (ThreadLocal).
scope.clearExternal();
}
}

UserTransaction ut = userTransaction();
Expand Down Expand Up @@ -198,6 +205,7 @@ public void afterCompletion(int status) {
log.log(DEBUG, "Jta Txn [{0}] rollback", transaction.id());
transaction.postRollback(null);
// Remove this transaction object as it is completed
transaction.setActive(false);
transactionManager.scope().clearExternal();
break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ public boolean isActive() {
return false;
}

@Override
public void setActive(boolean active) {

}


@Override
public void commitAndContinue() {
// do nothing
Expand Down

0 comments on commit a7dafa0

Please sign in to comment.