:toc: macro
toc::[]
= Transaction Handling
For transaction handling we link:guide-aop[AOP] to add transaction control via annotations as aspect.
This is done by annotating your code with the `@Transactional` annotation.
You can either annotate your link:guide-dependency-injection#key-principles[container bean] at class level to make all methods transactional or your can annotate individual methods to make them transactional:
[source,java]
----
@Transactional
public Output getData(Input input) {
...
}
----
== JTA Imports
Here are the import statements for transaction support:
[source, java]
----
import javax.transaction.Transactional;
----
CAUTION: Use the above import statement to follow JEE and avoid using `org.springframework.transaction.annotation.Transactional`.
== JTA Dependencies
Please note that with https://jakarta.ee/[Jakarta EE] the dependencies have changed.
When you want to start with Jakarta EE you should use these dependencies to get the annoations for dependency injection:
[source, xml]
----
jakarta.transaction
jakarta.transaction-api
----
Please note that with link:quarkus[quarkus] you will get them as transitive dependencies out of the box.
The above Jakarate EE dependencies replace these JEE depdencies:
[source, xml]
----
javax.transaction
javax.transaction-api
----
== Handling constraint violations
Using `@Transactional` magically wraps transaction handling around your code.
As constraints are checked by the database at the end when the transaction gets committed, a constraint violation will be thrown by this aspect outside your code.
In case you have to handle constraint violations manually, you have to do that in code outside the logic that is annotated with `@Transactional`.
This may be done in a link:guide-service-layer[service operation] by catching a `ConstraintViolationException` (`org.hibernate.exception.ConstraintViolationException` for hibernate).
As a generic approach you can solve this via link:guide-rest#rest-exception-handling[REST execption handling].
== Batches
Transaction control for batches is a lot more complicated and is described in the link:guide-batch-layer[batch layer].