Skip to content

guide transactions

devonfw-core edited this page Dec 13, 2022 · 6 revisions

Transaction Handling

For transaction handling we 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 container bean at class level to make all methods transactional or your can annotate individual methods to make them transactional:

  @Transactional
  public Output getData(Input input) {
    ...
  }

JTA Imports

Here are the import statements for transaction support:

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 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:

<!-- Java Transaction API (JTA) -->
<dependency>
  <groupId>jakarta.transaction</groupId>
  <artifactId>jakarta.transaction-api</artifactId>
</dependency>

Please note that with quarkus you will get them as transitive dependencies out of the box. The above Jakarate EE dependencies replace these JEE depdencies:

<!-- Java Transaction API (JTA) -->
<dependency>
  <groupId>javax.transaction</groupId>
  <artifactId>javax.transaction-api</artifactId>
</dependency>

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 service operation by catching a ConstraintViolationException (org.hibernate.exception.ConstraintViolationException for hibernate). As a generic approach you can solve this via REST execption handling.

Batches

Transaction control for batches is a lot more complicated and is described in the batch layer.

Clone this wiki locally