forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
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 quarkusio#28709
- Loading branch information
Showing
8 changed files
with
215 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
70 changes: 70 additions & 0 deletions
70
...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,70 @@ | ||
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; | ||
|
||
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(getTransactionId()); | ||
} | ||
|
||
@Override | ||
public void commit() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SecurityException, | ||
IllegalStateException, SystemException { | ||
TransactionId id = getTransactionId(); | ||
beforeDestroyed(id); | ||
try { | ||
delegate.commit(); | ||
} finally { | ||
destroyed(id); | ||
} | ||
} | ||
|
||
@Override | ||
public void rollback() throws IllegalStateException, SecurityException, SystemException { | ||
TransactionId id = getTransactionId(); | ||
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); | ||
} | ||
|
||
} |
90 changes: 90 additions & 0 deletions
90
...-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,90 @@ | ||
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.SystemException; | ||
import javax.transaction.TransactionScoped; | ||
|
||
import com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple; | ||
|
||
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(TransactionId transactionId) { | ||
if (initialized == null) { | ||
initialized = Arc.container().beanManager().getEvent() | ||
.select(TransactionId.class, Initialized.Literal.of(TransactionScoped.class)); | ||
} | ||
initialized.fire(transactionId); | ||
} | ||
|
||
void beforeDestroyed(TransactionId transactionId) { | ||
if (beforeDestroyed == null) { | ||
beforeDestroyed = Arc.container().beanManager().getEvent() | ||
.select(TransactionId.class, BeforeDestroyed.Literal.of(TransactionScoped.class)); | ||
} | ||
beforeDestroyed.fire(transactionId); | ||
} | ||
|
||
void destroyed(TransactionId transactionId) { | ||
if (destroyed == null) { | ||
destroyed = Arc.container().beanManager().getEvent() | ||
.select(TransactionId.class, Destroyed.Literal.of(TransactionScoped.class)); | ||
} | ||
destroyed.fire(transactionId); | ||
} | ||
|
||
TransactionId getTransactionId() throws SystemException { | ||
try { | ||
return new TransactionId(TransactionImple.getTransaction().toString()); | ||
} catch (Exception e) { | ||
throw new SystemException("The transaction is not active!"); | ||
} | ||
} | ||
|
||
// 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.