Skip to content

Commit 6229a7e

Browse files
author
Tomáš Kraus
committed
Helidon Transaction docs.
Signed-off-by: Tomáš Kraus <[email protected]>
1 parent a3ce31c commit 6229a7e

File tree

4 files changed

+209
-11
lines changed

4 files changed

+209
-11
lines changed

docs/src/main/asciidoc/includes/data.adoc

Lines changed: 164 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ defined by the `@Data.Query` annotation:
189189
include::{sourcedir}/includes/data/PetRepository.java[tag=PetRepository_class, indent=0]
190190
----
191191
192-
==== Method with query defined by method name
192+
==== Method with Query Defined by Method Name
193193
194194
This method type infers the query based on the method name and does not use a specific annotation.
195195
The method name must follow the _query-by-method-name_ syntax:
@@ -230,7 +230,7 @@ The method name must follow the _query-by-method-name_ syntax:
230230
direction :: "Asc" | "Desc"
231231
----
232232
233-
===== Method name prefix and return type
233+
===== Method Name Prefix and Return Type
234234
235235
A method can have a user-defined prefix. The prefix is a sequence of letters and digits that does not
236236
match any `<action-u>` keyword. This prefix has no influence on the query and can be used to distinguish
@@ -280,7 +280,7 @@ The return type depends on the action keyword:
280280
NOTE: Validation of the keyword–return type mapping is not fully enforced by the code generator,
281281
though this may change in future releases.
282282
283-
===== Projection in method name
283+
===== Projection in Method Name
284284
285285
The projection part is optional and follows directly after the return-type keyword. It consists
286286
of `expression` and `property` components:
@@ -323,7 +323,7 @@ translates to the JPQL query `SELECT p.keeper.name FROM Pet p`.
323323
include::{sourcedir}/includes/data/SimpleSnippets.java[tag=simple_model, indent=0]
324324
----
325325
326-
===== Criteria in method name
326+
===== Criteria in Method Name
327327
328328
The criteria part of the method name is optional and represents the `WHERE` clause of the query.
329329
It is a logical expression composed of individual conditions joined by the `AND` and `OR` operators.
@@ -452,7 +452,7 @@ Logical operators:
452452
NOTE: In JPQL, operator precedence places `AND` above `OR` as defined in Jakarta Persistence 3.1, section 4.6.6.
453453
The same rule applies to SQL.
454454
455-
===== Ordering in method name
455+
===== Ordering in Method Name
456456
457457
The ordering part of the method name is optional and represents the `ORDER BY` clause of the query.
458458
It is a list of ordering rules. A single ordering rule is the `property` optionally followed by
@@ -481,7 +481,7 @@ An example repository method with ordering:
481481
include::{sourcedir}/includes/data/PetRepositorySnippets.java[tag=qbmn_sort_method, indent=0]
482482
----
483483
484-
==== Method with query defined by `@Data.Query` annotation
484+
==== Method with Query Defined by `@Data.Query` Annotation
485485
486486
This method type must be annotated with `@Data.Query`. The annotation takes a single `String` value containing
487487
the database query. Currently, JPQL is supported. Method arguments must match the query parameters:
@@ -589,4 +589,161 @@ The session instance is available through the `Data.SessionRepository<S>` interf
589589
NOTE: The session instance is valid only while `run` or `call` method is being executed. This instance must not
590590
be stored and used after this method has ended.
591591
592-
// end::data_session_access_2[]
592+
// end::data_session_access_2[]
593+
594+
// tag::data_transaction[]
595+
596+
== Transactions
597+
598+
Transaction handling is available through Helidon Transaction API.
599+
600+
To enable Helidon Transaction API, add the following dependency to your project’s `pom.xml`:
601+
602+
[source,xml]
603+
----
604+
<dependency>
605+
<groupId>jakarta.transaction</groupId>
606+
<artifactId>jakarta.transaction-api</artifactId>
607+
</dependency>
608+
----
609+
610+
Helidon JTA Transaction support, such as Narayana, may be provided at runtime to enable `JTA` transaction type:
611+
612+
[source,xml]
613+
----
614+
<dependency>
615+
<groupId>io.helidon.transaction</groupId>
616+
<artifactId>helidon-transaction-narayana</artifactId>
617+
<scope>runtime</scope>
618+
</dependency>
619+
----
620+
621+
If JTA transaction support is not provided, Helidon Data runtime will use `RESOURCE_LOCAL` transaction type.
622+
623+
=== Transaction Types
624+
625+
The `Tx` class defines several ways how transactional support can be applied to transactional method executions.
626+
Those ways are defined in `Tx.Type` enum.
627+
628+
==== Transaction Type Enum
629+
The `Tx.Type` enum is used to describe the possible ways in which transactional support must be applied
630+
to transactional method executions.
631+
632+
===== Enum Values and Their Meanings
633+
634+
The Type enum has six values, each representing a different transactional behavior:
635+
636+
[cols=2*]
637+
|===
638+
|Name |Description
639+
640+
|`MANDATORY`
641+
|A transaction must already be in effect when a method executes. If called outside a transaction
642+
context, a `TxException` is thrown. If called inside a transaction context, method execution continues
643+
under that context.
644+
645+
|`NEW`
646+
|A new transaction is started when a method executes. If called outside a transaction context, a new
647+
transaction is begun. If called inside a transaction context, the current transaction is suspended, a new
648+
transaction is begun, and the method execution continues inside this new transaction context.
649+
650+
|`NEVER`
651+
|No transaction must be in effect when a method executes. If called outside a transaction context, method
652+
execution continues outside a transaction context. If called inside a transaction context, a `TxException`
653+
is thrown.
654+
655+
|`REQUIRED`
656+
|A transaction will be in effect when a method executes. If called outside a transaction context,
657+
a new transaction is begun. If called inside a transaction context, method execution continues inside
658+
that transaction context.
659+
660+
|`SUPPORTED`
661+
|A transaction may optionally be in effect when a method executes. If called outside a transaction
662+
context, method execution continues outside a transaction context. If called inside a transaction context,
663+
method execution continues inside that transaction context.
664+
665+
|`UNSUPPORTED`
666+
|No transaction will be in effect when a method executes. If called outside a transaction context, method
667+
execution continues outside a transaction context. If called inside a transaction context, the current
668+
transaction is suspended, method execution continues outside a transaction context, and the previously
669+
suspended transaction is resumed after method execution completes.
670+
|===
671+
672+
=== Transaction Annotations
673+
674+
The `Tx` class defines several annotations that can be used to mark methods for transactional execution:
675+
676+
[cols=2*]
677+
|===
678+
|Annotation |Description
679+
680+
|`@Mandatory`
681+
|Indicates that a method will be executed with a managed transaction of type MANDATORY.
682+
683+
|`@New`
684+
|Indicates that a method will be executed with a managed transaction of type NEW.
685+
686+
|`@Never`
687+
|Indicates that a method will be executed with a managed transaction of type NEVER.
688+
689+
|`@Required`
690+
|Indicates that a method will be executed with a managed transaction of type REQUIRED.
691+
692+
|`@Supported`
693+
|Indicates that a method will be executed with a managed transaction of type SUPPORTED.
694+
695+
|`@Unsupported`
696+
|Indicates that a method will be executed with a managed transaction of type UNSUPPORTED.
697+
|===
698+
699+
=== Transaction Methods
700+
701+
The `Tx` class provides several methods for executing tasks within a transaction:
702+
703+
[cols=2*]
704+
|===
705+
|Annotation |Description
706+
707+
|`transaction(Callable<T> task)`
708+
|Executes a task with a managed transaction of type `REQUIRED`.
709+
710+
|`transaction(Type type, Callable<T> task)`
711+
|Executes a task with a managed transaction of the specified type.
712+
713+
|`transaction(CheckedRunnable<Exception> task)`
714+
|Executes a task with a managed transaction of type `REQUIRED` without returning a result.
715+
716+
|`transaction(Type type, CheckedRunnable<Exception> task)`
717+
|Executes a task with a managed transaction of the specified type without returning a result.
718+
|===
719+
720+
=== Usage
721+
722+
The `Tx.Type` enum is used to control the transactional behavior of methods. By specifying the desired transactional
723+
behavior using one of the enum values, developers can ensure that their methods are executed with the correct transactional
724+
context.
725+
For example, using `REQUIRED` ensures that a method is always executed within a transaction, while using `NEVER` ensures
726+
that a method is never executed within a transaction.
727+
728+
In this example, the `doSomething()` method is annotated with `@Tx.Required`, ensuring that it is always executed within
729+
a transaction. The `doSomethingElse()` method is annotated with `@Tx.Never`, ensuring that it is never executed within
730+
a transaction:
731+
732+
[source,java]
733+
----
734+
include::{sourcedir}/includes/data/SimpleSnippets.java[tag=data_transaction_annotations, indent=0]
735+
----
736+
737+
`PetService` class instance is obtained from service registry.
738+
739+
In this example, lambda expression in the `doSomething()` method is executed using `Tx.transaction` method with
740+
`Tx.Type.REQUIRED` argument, ensuring that it is always executed within a transaction. Lambda expression
741+
in the `doSomethingElse()` method is executed using `Tx.transaction` method with `Tx.Type.NEVER` argument, ensuring
742+
that it is never executed within a transaction:
743+
744+
[source,java]
745+
----
746+
include::{sourcedir}/includes/data/SimpleSnippets.java[tag=data_transaction_methods, indent=0]
747+
----
748+
749+
// end::data_transaction[]

docs/src/main/asciidoc/mp/data.adoc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ include::{rootdir}/includes/mp.adoc[]
3737
- <<Pagination, Pagination>>
3838
- <<Dynamic Ordering, Dynamic Ordering>>
3939
- <<Persistence Session Access, Persistence Session Access>>
40+
- <<Transactions, Transactions>>
4041
4142
== Overview
4243
@@ -49,8 +50,8 @@ include::{rootdir}/includes/data.adoc[tag=data_config]
4950
5051
== MP Application
5152
52-
Data repository runtime initialization is handled by service registry. Repository interface instances can be obtained using
53-
`@Inject` annotation:
53+
The runtime initialization of the data repository is managed by the service registry. You can obtain repository
54+
interface instances using the `@Inject` annotation:
5455
5556
[source,java]
5657
----
@@ -67,3 +68,5 @@ include::{rootdir}/includes/data.adoc[tag=data_session_access_1]
6768
include::{sourcedir}/mp/data/PetService.java[tag=session_access, indent=0]
6869
----
6970
include::{rootdir}/includes/data.adoc[tag=data_session_access_2]
71+
72+
include::{rootdir}/includes/data.adoc[tag=data_transaction]

docs/src/main/asciidoc/se/data.adoc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ include::{rootdir}/includes/se.adoc[]
3636
- <<Pagination, Pagination>>
3737
- <<Dynamic Ordering, Dynamic Ordering>>
3838
- <<Persistence Session Access, Persistence Session Access>>
39+
- <<Transactions, Transactions>>
3940
4041
== Overview
4142
@@ -48,8 +49,8 @@ include::{rootdir}/includes/data.adoc[tag=data_config]
4849
4950
== SE Application
5051
51-
Data repository runtime initialization is handled by service registry. Repository interface instances can be obtained using
52-
service registry API:
52+
The runtime initialization of the data repository is managed by the service registry. You can obtain repository
53+
interface instances using the service registry API:
5354
5455
[source,java]
5556
----
@@ -66,3 +67,5 @@ include::{rootdir}/includes/data.adoc[tag=data_session_access_1]
6667
include::{sourcedir}/se/data/PetService.java[tag=session_access, indent=0]
6768
----
6869
include::{rootdir}/includes/data.adoc[tag=data_session_access_2]
70+
71+
include::{rootdir}/includes/data.adoc[tag=data_transaction]

docs/src/main/java/io/helidon/docs/includes/data/SimpleSnippets.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import java.util.List;
1919

2020
import io.helidon.data.Data;
21+
import io.helidon.service.registry.Service;
22+
import io.helidon.transaction.Tx;
2123

2224
import jakarta.persistence.Entity;
2325
import jakarta.persistence.EntityManager;
@@ -49,4 +51,37 @@ public interface KeeperRepository
4951
}
5052
// end::session_repository[]
5153

54+
// tag::data_transaction_annotations[]
55+
@Service.Singleton
56+
public class PetService {
57+
@Tx.Required
58+
public void doSomething() {
59+
// Method execution will always be within a transaction
60+
}
61+
62+
@Tx.Never
63+
public void doSomethingElse() {
64+
// Method execution will never be within a transaction
65+
}
66+
}
67+
// end::data_transaction_annotations[]
68+
69+
// tag::data_transaction_methods[]
70+
public class KeeperService {
71+
public void doSomething() {
72+
Tx.transaction(Tx.Type.REQUIRED,
73+
() -> {
74+
// Method execution will always be within a transaction
75+
});
76+
}
77+
78+
public void doSomethingElse() {
79+
Tx.transaction(Tx.Type.NEVER,
80+
() -> {
81+
// Method execution will never be within a transaction
82+
});
83+
}
84+
}
85+
// end::data_transaction_methods[]
86+
5287
}

0 commit comments

Comments
 (0)