Skip to content

guide transactions

devonfw-core edited this page Nov 21, 2022 · 6 revisions

Transaction Handling

Warning
Hey there! Seems like you are still using the documentation of our legacy Java repository. Since it won’t be maintained anymore, we recommend you to checkout the new Java page here.

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

Warning
Hey there! Seems like you are still using the documentation of our legacy Java repository. Since it won’t be maintained anymore, we recommend you to checkout the new Java page here. 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

Warning
Hey there! Seems like you are still using the documentation of our legacy Java repository. Since it won’t be maintained anymore, we recommend you to checkout the new Java page here. 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

Warning
Hey there! Seems like you are still using the documentation of our legacy Java repository. Since it won’t be maintained anymore, we recommend you to checkout the new Java page here. 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

Warning
Hey there! Seems like you are still using the documentation of our legacy Java repository. Since it won’t be maintained anymore, we recommend you to checkout the new Java page here. Transaction control for batches is a lot more complicated and is described in the batch layer.
Clone this wiki locally