Skip to content

Commit

Permalink
Merge pull request #3118 from ebean-orm/feature/3109-JtaTransaction-c…
Browse files Browse the repository at this point in the history
…urrentTransaction

#3109 JtaTransactionManager - fix getCurrentTransaction()
  • Loading branch information
rbygrave authored Jun 20, 2023
2 parents 3d3e5c2 + fa1f225 commit 9ddadb1
Showing 1 changed file with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private DataSource dataSource() {
return transactionManager.dataSource();
}

private TransactionSynchronizationRegistry getSyncRegistry() {
private TransactionSynchronizationRegistry registry() {
try {
InitialContext ctx = new InitialContext();
return (TransactionSynchronizationRegistry) ctx.lookup("java:comp/TransactionSynchronizationRegistry");
Expand All @@ -61,7 +61,7 @@ private TransactionSynchronizationRegistry getSyncRegistry() {
}
}

private UserTransaction getUserTransaction() {
private UserTransaction userTransaction() {
try {
InitialContext ctx = new InitialContext();
return (UserTransaction) ctx.lookup("java:comp/UserTransaction");
Expand All @@ -78,11 +78,16 @@ private UserTransaction getUserTransaction() {
*/
@Override
public Object getCurrentTransaction() {
TransactionSynchronizationRegistry syncRegistry = getSyncRegistry();
SpiTransaction t = (SpiTransaction) syncRegistry.getResource(EBEAN_TXN_RESOURCE);
if (t != null) {
// we have already seen this transaction
return t;
TransactionSynchronizationRegistry syncRegistry = registry();
try {
SpiTransaction t = (SpiTransaction) syncRegistry.getResource(EBEAN_TXN_RESOURCE);
if (t != null) {
// we have already seen this transaction
return t;
}
} catch (Exception e) {
// deem that there is no current transaction
return null;
}

// check current Ebean transaction
Expand All @@ -93,7 +98,7 @@ public Object getCurrentTransaction() {
return currentEbeanTransaction;
}

UserTransaction ut = getUserTransaction();
UserTransaction ut = userTransaction();
if (ut == null) {
// no current JTA transaction
if (log.isLoggable(DEBUG)) {
Expand Down

0 comments on commit 9ddadb1

Please sign in to comment.