Skip to content

Commit

Permalink
Stop Narayana getting initialized in static init
Browse files Browse the repository at this point in the history
This stops it being runtime configurable, and also loads the XML parsers
in JVM mode.
  • Loading branch information
stuartwdouglas committed Jul 26, 2021
1 parent 131d86f commit b1de0b5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package io.quarkus.narayana.interceptor;

import java.util.Collections;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.transaction.RollbackException;
Expand All @@ -16,15 +20,20 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.bootstrap.classloading.ClassLoaderLimiter;
import io.quarkus.test.QuarkusUnitTest;

public class TransactionalTest {

static final Set<String> loadedClasses = Collections.newSetFromMap(new ConcurrentHashMap<>());

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(TransactionalTest.TransactionalBean.class, TestXAResource.class,
TxAssertionData.class, TestException.class));
TxAssertionData.class, TestException.class))
.addClassLoaderEventListener(ClassLoaderLimiter.builder()
.neverLoadedClassName("javax.xml.stream.XMLInputFactory").build());

@Inject
private TransactionManager tm;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

@Dependent
public class NarayanaJtaProducers {
private static final javax.transaction.UserTransaction USER_TRANSACTION = UserTransaction.userTransaction();

@Produces
@ApplicationScoped
Expand All @@ -29,7 +28,7 @@ public UserTransactionRegistry userTransactionRegistry() {
@Produces
@ApplicationScoped
public javax.transaction.UserTransaction userTransaction() {
return USER_TRANSACTION;
return UserTransaction.userTransaction();
}

@Produces
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import javax.enterprise.context.ContextNotActiveException;
Expand All @@ -24,6 +25,7 @@
import io.quarkus.arc.InjectableBean;
import io.quarkus.arc.InjectableContext;
import io.quarkus.arc.impl.ContextInstanceHandleImpl;
import io.quarkus.arc.impl.LazyValue;

/**
* {@link javax.enterprise.context.spi.Context} class which defines the {@link TransactionScoped} context.
Expand All @@ -32,16 +34,27 @@ public class TransactionContext implements InjectableContext {
// marker object to be put as a key for SynchronizationRegistry to gather all beans created in the scope
private static final Object TRANSACTION_CONTEXT_MARKER = new Object();

private final TransactionSynchronizationRegistry transactionSynchronizationRegistry = new TransactionSynchronizationRegistryImple();
private final TransactionManager transactionManager = com.arjuna.ats.jta.TransactionManager.transactionManager();
private final LazyValue<TransactionSynchronizationRegistry> transactionSynchronizationRegistry = new LazyValue<>(
new Supplier<TransactionSynchronizationRegistry>() {
@Override
public TransactionSynchronizationRegistry get() {
return new TransactionSynchronizationRegistryImple();
}
});
private final LazyValue<TransactionManager> transactionManager = new LazyValue<>(new Supplier<TransactionManager>() {
@Override
public TransactionManager get() {
return com.arjuna.ats.jta.TransactionManager.transactionManager();
}
});

@Override
public void destroy() {
if (!isActive()) {
return;
}

TransactionContextState contextState = (TransactionContextState) transactionSynchronizationRegistry
TransactionContextState contextState = (TransactionContextState) transactionSynchronizationRegistry.get()
.getResource(TRANSACTION_CONTEXT_MARKER);
if (contextState == null) {
return;
Expand All @@ -54,7 +67,7 @@ public void destroy(Contextual<?> contextual) {
if (!isActive()) {
return;
}
TransactionContextState contextState = (TransactionContextState) transactionSynchronizationRegistry
TransactionContextState contextState = (TransactionContextState) transactionSynchronizationRegistry.get()
.getResource(TRANSACTION_CONTEXT_MARKER);
if (contextState == null) {
return;
Expand All @@ -69,7 +82,7 @@ public ContextState getState() {
}

ContextState result;
TransactionContextState contextState = (TransactionContextState) transactionSynchronizationRegistry
TransactionContextState contextState = (TransactionContextState) transactionSynchronizationRegistry.get()
.getResource(TRANSACTION_CONTEXT_MARKER);
if (contextState == null) {
result = new TransactionContextState(getCurrentTransaction());
Expand All @@ -95,12 +108,12 @@ public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContex
}

TransactionContextState contextState;
contextState = (TransactionContextState) transactionSynchronizationRegistry
contextState = (TransactionContextState) transactionSynchronizationRegistry.get()
.getResource(TRANSACTION_CONTEXT_MARKER);

if (contextState == null) {
contextState = new TransactionContextState(getCurrentTransaction());
transactionSynchronizationRegistry.putResource(TRANSACTION_CONTEXT_MARKER, contextState);
transactionSynchronizationRegistry.get().putResource(TRANSACTION_CONTEXT_MARKER, contextState);
}

ContextInstanceHandle<T> instanceHandle = contextState.get(contextual);
Expand Down Expand Up @@ -150,7 +163,7 @@ public boolean isActive() {

private Transaction getCurrentTransaction() {
try {
return transactionManager.getTransaction();
return transactionManager.get().getTransaction();
} catch (SystemException e) {
throw new RuntimeException("Error getting the current transaction", e);
}
Expand Down

0 comments on commit b1de0b5

Please sign in to comment.