Setting Up Transaction Support

This guide shows how to configure and use Java Transaction API (JTA)-compliant transactions in your Helidon MP application.

What You Need

About 10 minutes
Helidon Prerequisites

Add The Helidon JTA Integration to Your Helidon MP Application’s Runtime Classpath

To bring JTA transactions to your Helidon MP application, you’ll need to add the relevant extension. Specifically, you’ll need to add an appropriate <dependency> element as a child element of the <dependencies> element in your pom.xml, referencing the Helidon JTA extension:

<dependency>
    <groupId>io.helidon.integrations.cdi</groupId>
    <artifactId>helidon-integrations-cdi-jta-weld</artifactId>
    <version>{helidon-version}</version>
    <scope>runtime</scope> 
</dependency>
Copied
  • Note the scope is runtime.

Add JTA to Your Helidon MP Application’s Compilation Classpath

To actually use the Java Transaction API in your code, you’ll need to ensure a library defining the classes and interfaces mandated by the specification is present on your compilation classpath. (Note that this library is separate from any given vendor’s actual implementation of the specification by way of these classes and interfaces.)

<dependency>
    <groupId>javax.transaction</groupId>
    <artifactId>javax.transaction-api</artifactId>
    <version>1.2</version>
    <scope>provided</scope> 
</dependency>
Copied
  • The scope is provided to allow the JTA implementation runtime to provide its own implementation of the API jar if necessary.

Annotate a Method With @Transactional

Choose a method that you wish to have a certain kind of transactional behavior, and annotate it with the @Transactional annotation.

The method in question will need to be a business method of some kind: a method that is invoked by the Helidon MP server machinery, not directly by the user. This is because normally the behavior that @Transactional requests is provided by interceptor functionality. More concretely, in Helidon MP you can annotate a JAX-RS resource method, or a method on a CDI bean that itself is injected in your application somewhere.

For example, a method on a hypothetical PersonDAO class that saves a hypothetical Person object to a database, starting a new JTA transaction if necessary, might look like this:

PersonDAO.java
import javax.transaction.Transactional;
import javax.transaction.Transactional.TxType;

@Transactional(TxType.REQUIRED) 
public void savePerson(Person person) {
    // Use JPA or another JTA-aware framework to save the Person object 
}
Copied
  • The Transactional annotation indicates the kind of transactional behavior you would like this method to have. In this example, we explicitly set the kind of behavior to be REQUIRED (which also happens to be the default if you do not specify an explicit TxType).
  • Annotating a method with @Transactional demarcates a JTA transaction, but it is up to individual JTA-aware frameworks and libraries to actually do something when the transaction is implicitly started. JPA is an example of a framework that is JTA aware.