question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"Enlisted connection used without active transaction" during committing transactions with active async threads

See original GitHub issue

Describe the bug

Hi guys! I’ve found out recently that not all of my transactions are committed. The small investigation led me to believe there is a problem in the commit transaction process: when some of the async processing is not finished yet using CompletableStage/CompletableFuture, the transaction fails with the message “Error trying to transactionCommit local transaction: Enlisted connection used without active transaction”. I’ve attached a small reproducer for you with a simple test to check this behavior.

I know it’s better to finish all async execution before committing the transaction. But there are cases when it’s not under developer control. For example, in my project I have code like this:

@Transaction(REQUIRES_NEW)
public Optional<Integer> someMethod() {
	CompletableFuture<SomeResponse> timedResponse = new CompletableFuture<>();
        try {
            timedResponse = restClient.makeRequest().toCompletableFuture();
            SomeResponse response = timedResponse.get(TIMEOUT_CONST, MILLISECONDS);

            repository.persist(new Entity(response.getId));
            return Optional.of(response.getId());
        } catch (Exception ex) {
            try {
                // cancel rest request future if not completed yet
                timedResponse.cancel(true);
            } catch (Exception ignored) {}
            return Optional.empty();
        }
}

It’s just some pseudo-code example of how hard it is to control async execution here. And from time to time I receive 500 exception during rest request (that is OK and expected). But the problem is that there are cases when I receive a warning “commiting with 2 threads active!” and right after this exception “Enlisted connection used without active transaction”. Please see the reproducer.

Expected behavior

The transaction is committed with a warning may be but without exception.

Actual behavior

The transaction is not committed with warning and exception “Error trying to transactionCommit local transaction: Enlisted connection used without active transaction”

How to Reproduce?

transaction_commit_fail.zip

  1. Run TransactionCommitTest.test()
  2. Check that transaction is not committed and there is an exception in logs

Output of uname -a or ver

No response

Output of java -version

java version “11.0.12” 2021-07-20 LTS

GraalVM version (if different from Java)

No response

Quarkus version or git rev

2.6.1.Final

Build tool (ie. output of mvnw --version or gradlew --version)

No response

Additional information

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:16 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
ahofmeistercommented, Apr 8, 2022

Got the same problem, but with the Emitter from smallrye-reactive-messaging-kafka

3reactions
lamemind-udcommented, Apr 10, 2022

Same here. I’m emitting some data with smallrye-reactive-messaging-kafka then I want to update the DB, but I can’t.

I managed to write to the database using the Event Bus. And even if my code is working, it throws anyway: javax.transaction.xa.XAException: Error trying to transactionCommit local transaction: Enlisted connection used without active transaction

Here is some example code…

@ApplicationScoped
public class KafkaProduceThenUpdate {

    @Inject
    @Channel("kafka-channel-out")
    Emitter<User> emitter;

    @Inject
    UserRepository userRepository;

    @Scheduled(every = "10s", concurrentExecution = Scheduled.ConcurrentExecution.SKIP)
    @Transactional
    public void generate() {
        userRepository.getNextToSyncToKafka().ifPresent(user -> {
            System.out.println("Emitting User " + user);
            emitter.send(user).thenAccept(unused -> {
                bus.send("greeting", user);
            });
        });
    }

    @Inject
    EventBus bus;

    @ConsumeEvent(value = "greeting", blocking = true)
    @Transactional
    public void consume(User user) {
        userRepository.setSentToKafka(user);
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Random "Enlisted connection used without active transaction ...
I worked around this issue by firing an event which is listened in a TransactionPhase.AFTER_SUCCESS method (and this method send the Kafka message)....
Read more >
Enlisted connection used without active transaction
I think it means that you have started using the EntityManager inside a transaction, then you have used it with no TX active...
Read more >
Using Transactions in Quarkus
JOIN_EXISTING. If no transaction is active then a new transaction will be started, and committed when the method ends.
Read more >
Transactions Development Guide - Red Hat Customer Portal
In a multi-threaded environment, multiple threads may be active within the same transaction. If checked transaction semantics have been disabled, ...
Read more >
147 Transaction Control Service Specification - OSGi Docs
Scoped Resources are always thread-safe, and can be used concurrently in different ... When enlisted in an Active Transaction a JDBC connection will...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found