Quarkus Transaction Timeout configuration

Quarkus is a popular framework for building efficient and scalable Java applications. One critical aspect of application development is managing transactions, and Quarkus provides flexible ways to configure transaction timeouts. In this article, we’ll explore how to configure transaction timeouts in Quarkus.

Default Transaction Timeout

The Narayana JTA transaction manager lets you coordinate and expose JTA transactions to your Quarkus applications. To learn how to configure Transaction in Quarkus we recommend checking this article: How to manage Transactions in Quarkus

Quarkus allows you to set a default transaction timeout for your entire application. This timeout specifies how long a transaction can remain active before it’s automatically rolled back. By default, the default transaction timeout is set to 60 seconds.

Configuration Property

You can configure the default transaction timeout by using the quarkus.transaction-manager.default-transaction-timeout configuration property.

The default Transaction Timeout is set to 60 seconds as you can see from the Quarkus Properties List:

quarkus transaction timeout configuration

If your Transaction attempts to commit after the transaction timeout value you will see the following WARN Message on your logs:

2023-07-24 20:11:59,874 WARN  [com.arj.ats.arjuna] (Transaction Reaper) ARJUNA012117: TransactionReaper::check timeout for TX 0:ffff0a000020:d58d:5cdad26e:81 in state  RUN

2023-07-24 20:12:47,198 WARN  [com.arj.ats.arjuna] (DefaultQuartzScheduler_Worker-3) ARJUNA012077: Abort called on already aborted atomic action 0:ffff0a000020:d58d:5cdad26e:81

Caused by: javax.transaction.RollbackException: ARJUNA016102: The transaction is not active! Uid is 0:ffff0a000020:d58d:5cdad26e:81

To change the default timeout, you can add the following property to your application.properties or application.yaml file:

quarkus.transaction-manager.default-transaction-timeout=180

In the above example, we’ve set the default transaction timeout to 180 seconds (3 minutes).

It is also possible to set the time using the standard java.time.Duration format. For example, to set the timeout to 5 minutes, enter the following configuration:

quarkus.transaction-manager.default-transaction-timeout=PT5M

Transaction Timeout Annotation

While setting a default transaction timeout is useful for most cases, there might be scenarios where you need to specify a different timeout for a specific method. Quarkus provides the @TransactionConfiguration annotation for this purpose.

@TransactionConfiguration Annotation

To set a timeout property for a transaction created within an annotated method, you can use the @TransactionConfiguration annotation. Here’s how you can do it:

import io.quarkus.hibernate.orm.TransactionConfiguration;

@Transactional
@TransactionConfiguration(timeout = 40)
public void myTransactionalMethod() {
    // Your transactional logic here
}

In the above code snippet, we’ve annotated the myTransactionalMethod with @TransactionConfiguration(timeout = 40). This means that any transactions created within this method will have a timeout of 40 seconds, overriding the default transaction timeout.

Please note that you can apply the @TransactionConfiguration also at Class level. When you define @TransactionConfiguratio on a class, it is equivalent to defining it on all the @Transactional methods of the class.However, the configuration defined on a method takes precedence over the configuration defined on a class.

Practical Use Cases

Configuring transaction timeouts can be crucial in various scenarios:

  1. Long-Running Operations: If you have a specific method that performs a long-running operation within a transaction, you can set a longer timeout to accommodate the task’s duration.
  2. Critical Transactions: Some transactions might be more critical than others and require shorter timeouts to ensure timely completion or rollback in case of issues.
  3. Resource Management: When working with external resources like databases or web services, setting appropriate transaction timeouts can prevent resource leaks and ensure efficient resource utilization.

Conclusion

In Quarkus, you can easily configure transaction timeouts at both the application-wide and method-specific levels. This flexibility allows you to fine-tune your application’s transaction management to meet specific requirements, ensuring the reliability and efficiency of your application.

Found the article helpful? if so please follow us on Socials