-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UserTransaction should fire context lifecycle events
- such as @initialized(TransactionScoped.class) - resolves #28709
- Loading branch information
Showing
8 changed files
with
206 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...a-jta/runtime/src/main/java/io/quarkus/narayana/jta/runtime/NotifyingUserTransaction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package io.quarkus.narayana.jta.runtime; | ||
|
||
import javax.transaction.HeuristicMixedException; | ||
import javax.transaction.HeuristicRollbackException; | ||
import javax.transaction.NotSupportedException; | ||
import javax.transaction.RollbackException; | ||
import javax.transaction.SystemException; | ||
import javax.transaction.UserTransaction; | ||
|
||
import org.jboss.logging.Logger; | ||
|
||
import com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple; | ||
|
||
public class NotifyingUserTransaction extends TransactionScopedNotifier implements UserTransaction { | ||
|
||
private static final Logger LOG = Logger.getLogger(NotifyingUserTransaction.class); | ||
|
||
private final UserTransaction delegate; | ||
|
||
public NotifyingUserTransaction(UserTransaction delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
public void begin() throws NotSupportedException, SystemException { | ||
delegate.begin(); | ||
initialized(TransactionImple.getTransaction().toString()); | ||
} | ||
|
||
@Override | ||
public void commit() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SecurityException, | ||
IllegalStateException, SystemException { | ||
String id = TransactionImple.getTransaction().toString(); | ||
beforeDestroyed(id); | ||
try { | ||
delegate.commit(); | ||
} finally { | ||
destroyed(id); | ||
} | ||
} | ||
|
||
@Override | ||
public void rollback() throws IllegalStateException, SecurityException, SystemException { | ||
String id = TransactionImple.getTransaction().toString(); | ||
try { | ||
beforeDestroyed(id); | ||
} catch (Throwable t) { | ||
LOG.error("Failed to fire @BeforeDestroyed(TransactionScoped.class)", t); | ||
} | ||
try { | ||
delegate.rollback(); | ||
} finally { | ||
destroyed(id); | ||
} | ||
} | ||
|
||
@Override | ||
public void setRollbackOnly() throws IllegalStateException, SystemException { | ||
delegate.setRollbackOnly(); | ||
} | ||
|
||
@Override | ||
public int getStatus() throws SystemException { | ||
return delegate.getStatus(); | ||
} | ||
|
||
@Override | ||
public void setTransactionTimeout(int seconds) throws SystemException { | ||
delegate.setTransactionTimeout(seconds); | ||
} | ||
|
||
} |
79 changes: 79 additions & 0 deletions
79
...-jta/runtime/src/main/java/io/quarkus/narayana/jta/runtime/TransactionScopedNotifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package io.quarkus.narayana.jta.runtime; | ||
|
||
import java.util.Objects; | ||
|
||
import javax.enterprise.context.BeforeDestroyed; | ||
import javax.enterprise.context.Destroyed; | ||
import javax.enterprise.context.Initialized; | ||
import javax.enterprise.event.Event; | ||
import javax.transaction.TransactionScoped; | ||
|
||
import io.quarkus.arc.Arc; | ||
|
||
public abstract class TransactionScopedNotifier { | ||
|
||
private transient Event<TransactionId> initialized; | ||
private transient Event<TransactionId> beforeDestroyed; | ||
private transient Event<TransactionId> destroyed; | ||
|
||
void initialized(String transactionId) { | ||
if (initialized == null) { | ||
initialized = Arc.container().beanManager().getEvent() | ||
.select(TransactionId.class, Initialized.Literal.of(TransactionScoped.class)); | ||
} | ||
initialized.fire(new TransactionId(transactionId)); | ||
} | ||
|
||
void beforeDestroyed(String transactionId) { | ||
if (beforeDestroyed == null) { | ||
beforeDestroyed = Arc.container().beanManager().getEvent() | ||
.select(TransactionId.class, BeforeDestroyed.Literal.of(TransactionScoped.class)); | ||
} | ||
beforeDestroyed.fire(new TransactionId(transactionId)); | ||
} | ||
|
||
void destroyed(String transactionId) { | ||
if (destroyed == null) { | ||
destroyed = Arc.container().beanManager().getEvent() | ||
.select(TransactionId.class, Destroyed.Literal.of(TransactionScoped.class)); | ||
} | ||
destroyed.fire(new TransactionId(transactionId)); | ||
} | ||
|
||
// we use this wrapper because if we fire an event with string payload then any "@Observes String payload" would be notified | ||
public static final class TransactionId { | ||
|
||
private final String value; | ||
|
||
public TransactionId(String value) { | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(value); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
if (obj == null) { | ||
return false; | ||
} | ||
if (getClass() != obj.getClass()) { | ||
return false; | ||
} | ||
TransactionId other = (TransactionId) obj; | ||
return Objects.equals(value, other.value); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.