VOOZH about

URL: https://www.javacodegeeks.com/2018/01/transactional-exception-handling-cdi.html

⇱ Transactional exception handling in CDI - Java Code Geeks


In Java EE, exceptions that are raised during the execution of a transactional business method cause the transaction to rollback. However, this is only the case for system exceptions, that is, runtime exceptions, which are not declared in the method signature.

For application exceptions, that is, checked exceptions, or any exception annotated with @ApplicationException, the transaction is not automatically rolled back. This sometimes causes confusion among enterprise developers.

For EJB business methods, the transactions can be forced to roll back on application exceptions as well, by specifying @ApplicationException(rollback = true). However, this annotation is only considered if the managed bean in an EJB.

CDI also makes it possible to execute business methods transactionally, using @Transactional. This annotation gives us even more control. With @Transactional we can not only define the transaction type, such as REQUIRED or REQUIRES_NEW, but also on which exception types we do or do not want to rollback:

public class CarManufacturer {

 @Inject
 CarFactory carFactory;

 @Inject
 Event<CarCreated> createdCars;

 @PersistenceContext
 EntityManager entityManager;

 @Transactional(rollbackOn = CarCreationException.class,
 dontRollbackOn = NotificationException.class)
 public Car manufactureCar(Specification specification) {
 Car car = carFactory.createCar(specification);
 entityManager.persist(car);
 createdCars.fire(new CarCreated(car.getIdentification()));
 return car;
 }

}

The transaction will be rolled back in case a CarCreationException occurs, but not for NotificationExceptions.

Published on Java Code Geeks with permission by Sebastian Daschner, partner at our JCG program. See the original article here: Transactional exception handling in CDI

Opinions expressed by Java Code Geeks contributors are their own.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

πŸ‘ Photo of Sebastian Daschner
Sebastian Daschner
January 2nd, 2018Last Updated: January 2nd, 2018
0 122 1 minute read

Sebastian Daschner

Sebastian Daschner is a self-employed Java consultant and trainer. He is the author of the book 'Architecting Modern Java EE Applications'. Sebastian is a Java Champion, Oracle Developer Champion and JavaOne Rockstar.
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button
Close
wpDiscuz