How to increase the Transaction Timeout in JBoss / WildFly

Transaction timeout in WildFly / JBoss can be configured in the transactions subsystem or at EJB level. When the transaction is configured in the transactions subsystem it will be the default transaction timeout for all JTA transactions. When configured at EJB level, the timeout will be specific of that EJB.

Configuring Transaction timeout in the transactions subsystem

Out of box, the transactions subsystem does not show the default value of the JTA transaction timeout:

<subsystem xmlns="urn:jboss:domain:transactions:6.0">
    <core-environment node-identifier="${jboss.tx.node.id:1}">
        <process-id>
            <uuid/>
        </process-id>
    </core-environment>
    <recovery-environment socket-binding="txn-recovery-environment" status-socket-binding="txn-status-manager"/>
    <coordinator-environment statistics-enabled="${wildfly.transactions.statistics-enabled:${wildfly.statistics-enabled:false}}"/>
    <object-store path="tx-object-store" relative-to="jboss.server.data.dir"/>
</subsystem>

The value of default-timeout can be configured through the coordinator-environment element as follows:

<coordinator-environment default-timeout="300"/>

Here is how tou can increase the default transaction timeout with the CLI:

/subsystem=transactions:write-attribute(name=default-timeout,value=400)
{"outcome" => "success"}

Configuring the Transaction Timeout in EJBs

Firstly, you can set the transaction timeout for a specific EJB  with the @org.jboss.ejb3.annotation.TransactionTimeout annotation:

import java.util.concurrent.TimeUnit;
import org.jboss.ejb3.annotation.TransactionTimeout;

@Stateless
public class SampleBean 
{

  @TransactionTimeout(value = 30, unit = TimeUnit.SECONDS)
  public String doSomething() throws RuntimeException
  {
    //
  }
}

For Bean Managed Transactions, you can use the method setTransactionTimeout of the UserTransaction interface to set the timeout:

public void doSomething() {
    try {
        ut.setTransactionTimeout(600); // 10 minutes
        ut.begin();

Finally, you can also use the jboss-ejb3.xml descriptor to set the transaction timeout:

<jboss:ejb-jar xmlns:jboss="http://www.jboss.com/xml/ns/javaee"
               xmlns="http://java.sun.com/xml/ns/javaee"
               xmlns:tx="urn:trans-timeout"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://www.jboss.com/xml/ns/javaee http://www.jboss.org/j2ee/schema/jboss-ejb3-2_0.xsd
http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd
urn:trans-timeout http://www.jboss.org/j2ee/schema/trans-timeout-1_0.xsd"
               version="3.1"
               impl-version="2.0">
    <enterprise-beans>
        <session>
            <ejb-name>SampleBean</ejb-name>
            <ejb-class>com.acme.SampleBean</ejb-class>
            <session-type>Stateless</session-type>
        </session>
    </enterprise-beans>
    <assembly-descriptor>
        <container-transaction>
            <method>
                <ejb-name>SampleBean</ejb-name>
                <method-name>*</method-name>
                <method-intf>Local</method-intf>
            </method>
            <tx:trans-timeout>
                <tx:timeout>30</tx:timeout>
                <tx:unit>Seconds</tx:unit>
            </tx:trans-timeout>
        </container-transaction>
    </assembly-descriptor>
</jboss:ejb-jar>

On the other hand, if you are using Message Driven Beans, you can set the transactionTimeout as ActivationConfigProperty of your MDB:

@MessageDriven(name = "TestMDB", activationConfig = {
        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
        @ActivationConfigProperty(propertyName = "destination", propertyValue = "testQueue"),
        @ActivationConfigProperty(propertyName = "transactionTimeout", propertyValue="4")
        })

Transaction Timeout order

What happens when you have multiple points when a transaction timeout can happen? there is no hierarchy in this regards. Simply put, the first scope where a timeout happens that will cause the failure of a transaction.

Typically, the subsystem transaction timeout should be large enough to cover all application cases such as EJB Transaction timeouts.

Conclusion

In conclusion, configuring the transaction timeout in WildFly is a straightforward process that can be done by modifying the default-timeout attribute in the coordinator-environment element of the transactions subsystem in the standalone.xml configuration file. By increasing the transaction timeout, you can allow longer-running transactions to complete successfully, while still preventing transactions from taking too long and potentially causing resource contention or deadlocks.

if you want to know more details about configuring the Transactions using a JDBC Store instead of the default file store we recommend checking this article: How to configure a JDBC Store for Transactions

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