- Helidon MP JTA Guide
This guide shows how to configure and use Java Transaction API (JTA)-compliant transactions in your Helidon MP application.
What You Need
For this 10 minute tutorial, you will need the following:
| A Helidon {upper-case-flavor} Application | You can use your own application or use the Helidon {upper-case-flavor} Quickstart to create a sample application. |
| Java SE 11 (Open JDK 11) | Helidon requires Java 11+. |
| Maven 3.6.1+ | Helidon requires Maven 3.6.1+. |
| Docker 18.09+ | You need Docker if you want to build and deploy Docker containers. |
| Kubectl 1.16.5+ | If you want to deploy to Kubernetes, you need kubectl and a Kubernetes cluster (you can install one on your desktop). |
java -version
mvn --version
docker --version
kubectl version --short# On Mac
export JAVA_HOME=`/usr/libexec/java_home -v 11`
# On Linux
# Use the appropriate path to your JDK
export JAVA_HOME=/usr/lib/jvm/jdk-11Add the Helidon JTA Integration to Your 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>
<scope>runtime</scope>
</dependency>- Note the scope is
runtime.
Add JTA to Your 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>jakarta.transaction</groupId>
<artifactId>jakarta.transaction-api</artifactId>
<scope>provided</scope>
</dependency>- The scope is
providedto 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.javaimport 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
}- The
Transactionalannotation indicates the kind of transactional behavior you would like this method to have. In this example, we explicitly set the kind of behavior to beREQUIRED(which also happens to be the default if you do not specify an explicitTxType). - Annotating a method with
@Transactionaldemarcates 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.