How to set EJB transaction timeout in JBoss EAP 7 / 6
Environment
- Red Hat JBoss Enterprise Application Platform (EAP)
- 7
- 6
Issue
- How to set a transaction timeout in jboss ejb descriptor file?
- How to set a transaction timeout via annotations?
- How to set EJB transaction timeout in JBoss EAP 7 / 6
Resolution
The transaction timeout for an EJB can be configured via annotations or via the jboss-ejb3.xml.
Configuring the Transactions Timeout via annotations
import java.util.concurrent.TimeUnit;
import org.jboss.ejb3.annotation.TransactionTimeout;
@Stateless
public class HelloBean implements Hello, HelloLocal
{
@TransactionTimeout(value = 10, unit = TimeUnit.SECONDS)
public String hello(String name) throws HelloException
{
return "Hello " + name;
}
}
You can find the TransactionTimeout annotation class in jboss-ejb3-ext-api jar file which is typically located under org.jboss.ejb3 module:
$JBOSS_HOME/modules/layers/base/org/jboss/ejb3/main/jboss-ejb3-ext-api-2.1.0.redhat-1.jar
Configuring the Transaction Timeout via the jboss-ejb3.xml
Below is an example of jboss-ejb3.xml with transaction timeout. The jboss-ejb3.xml should be packaged in the META-INF of the EJB jar.
This approach can be used for legacy EJB2.x implementations as replacement for the (ignored) jboss.xml descriptor.
<?xml version="1.1" encoding="UTF-8"?>
<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>HelloBean</ejb-name>
<ejb-class>com.jboss.examples.ejb3.TestSLSBean</ejb-class>
<session-type>Stateless</session-type>
</session>
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method> <!-- For all local interface methods of TestSLSBean -->
<ejb-name>TestSLSBean</ejb-name>
<method-name>*</method-name>
<method-intf>Local</method-intf>
</method>
<tx:trans-timeout>
<tx:timeout>10</tx:timeout>
<tx:unit>Seconds</tx:unit>
</tx:trans-timeout>
</container-transaction>
</assembly-descriptor>
</jboss:ejb-jar>
Hint: there is a issue if local deployed beans are invoked via the RemoteInterface, see Content from issues.jboss.org is not included.WFLY-2789. This issue is fixed for EAP6.2.4 and EAP6.3+
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.