///////////////////////////////////////////////////////////////////////////////

    Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

///////////////////////////////////////////////////////////////////////////////

= CDI extension for JTA
:description: Helidon CDI extension for JTA
:keywords: helidon, java, microservices, microprofile, extensions, cdi, jta

This https://docs.jboss.org/cdi/spec/2.0/cdi-spec.html#spi[CDI
portable extension] provides support for JTA (Java Transaction API)
transactions in your Helidon MicroProfile applications.

== Prerequsites

Declare the following dependency fragment in your project's `pom.xml`:

[source,xml]
----
<dependency>
  <groupId>io.helidon.integrations.cdi</groupId>
  <artifactId>helidon-integrations-cdi-jta-weld</artifactId>
  <scope>runtime</scope>
</dependency>

<dependency>
  <groupId>javax.transaction</groupId>
  <artifactId>javax.transaction-api</artifactId>
  <scope>provided</scope>
</dependency>
----

== Declaring a method to be transactional

The following example shows how to declare a transactional method.

[source,java]
.Transactional method declaration
----
@Transactional(Transactional.TxType.REQUIRED)
public void doSomethingTransactionally() {

}
----

The extension ensures that a transaction is started before and
committed after the method executes.  If the method throws an
exception, the transaction will be rolled back.

You can further specify the transactional behavior of the extension by
using different instances of the `Transactional` annotation.  For more
information, see the
https://static.javadoc.io/javax.transaction/javax.transaction-api/1.2/javax/transaction/Transactional.html[`Transactional`
annotation documentation].

Transactional method support is implemented by CDI interception
facilities.  Among other things, this means that the method to which
you apply the `Transactional` annotation must not be `private` and
must in all other ways be a _business method_.  See the
https://jcp.org/aboutJava/communityprocess/mrel/jsr318/index3.html[Java
Interceptors specification] for more details.

During a transactional method invocation, the extension makes the
following objects available for injection via the `Inject` annotation:

* https://static.javadoc.io/javax.transaction/javax.transaction-api/1.2/javax/transaction/UserTransaction.html[`UserTransaction`]
* https://static.javadoc.io/javax.transaction/javax.transaction-api/1.2/javax/transaction/Transaction.html[`Transaction`]
* https://static.javadoc.io/javax.transaction/javax.transaction-api/1.2/javax/transaction/UserTransactionManager.html[`TransactionManager`]
* https://static.javadoc.io/javax.transaction/javax.transaction-api/1.2/javax/transaction/UserTransactionSynchronizationRegistry.html[`TransactionSynchronizationRegistry`]


