Skip to content

Commit

Permalink
Merge pull request #151 from grgrzybek/ARIES-2073
Browse files Browse the repository at this point in the history
[ARIES-2073] SUPPORTS TX attribute should not create additional coord…
  • Loading branch information
grgrzybek committed Apr 20, 2022
2 parents 5b27fe0 + b9a69d7 commit 9579cf3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@ public TransactionToken begin(TransactionManager man) throws SystemException, No
InvalidTransactionException
{
if (man.getStatus() == Status.STATUS_ACTIVE) {
return new TransactionToken(man.getTransaction(), null, SUPPORTS);
return new TransactionToken(man.getTransaction(), null, SUPPORTS, false, false);
}

return new TransactionToken(null, null, SUPPORTS);
return new TransactionToken(null, null, SUPPORTS, false, false);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Optional;

import javax.transaction.RollbackException;
import javax.transaction.Status;
import javax.transaction.Transaction;
import javax.transaction.TransactionManager;

Expand Down Expand Up @@ -61,7 +62,18 @@ public Object preCall(ComponentMetadata cm, Method m, Object... parameters) thro

LOGGER.debug("PreCall for bean {}, method {} with tx strategy {}.", getCmId(cm), m.getName(), txAttribute);
TransactionToken token = txAttribute.begin(tm);
if (token.requiresNewCoordination()) {
boolean requiresNewCoordination = token.requiresNewCoordination();
if (!requiresNewCoordination && !transactionExists(coordinator)) {
// in case the txAttribute doesn't require new coordination AND there's active transaction, like in:
// - REQUIRED, when there was TX active before the method
// - SUPPORTS, when there was TX active before the method
// - MANDATORY, when there was TX active before the method
// we still have to create coordination
if (token.getActiveTransaction() != null && tm.getStatus() == Status.STATUS_ACTIVE) {
requiresNewCoordination = true;
}
}
if (requiresNewCoordination) {
String coordName = "txInterceptor." + m.getDeclaringClass().getName() + "." + m.getName();
Coordination coord = coordinator.begin(coordName , 0);
// @javax.transaction.Transactional is only part of 1.2 and even if it's about time that all previous
Expand All @@ -72,6 +84,23 @@ public Object preCall(ComponentMetadata cm, Method m, Object... parameters) thro
return token;
}

/**
* Checks whether there's already a {@link Coordination} with {@link Transaction} variable
* @param coordinator
* @return
*/
private boolean transactionExists(Coordinator coordinator) {
boolean exists = false;
Coordination coord = coordinator.peek();
while (coord != null) {
if (coord.getVariables().containsKey(Transaction.class)) {
return true;
}
coord = coord.getEnclosingCoordination();
}
return exists;
}

@Override
public void postCallWithException(ComponentMetadata cm, Method m, Throwable ex, Object preCallToken) {
if (!(preCallToken instanceof TransactionToken)) {
Expand Down

0 comments on commit 9579cf3

Please sign in to comment.