1. Overview
In this tutorial, we’ll discuss the differences between org.springframework.transaction.annotation.Transactional and javax.transaction.Transactional annotations.
We’ll start with an overview of their configuration properties. Then, we’ll discuss what types of components each can be applied to, and in which circumstances we can use one or the other.
2. Configuration Differences
Spring’s Transactional annotation comes with additional configuration compared to its JTA counterpart:
- Isolation – Spring offers transaction-scoped isolation through the isolation property; however, in JTA, this feature is available only at a connection level
- Propagation – available in both libraries, through the propagation property in Spring, and the value property in Java EE; Spring offers Nested as an additional propagation type
- Read-Only – available only in Spring through the readOnly property
- Timeout – available only in Spring through the timeout property
- Rollback – both annotations offer rollback management; JTA provides the rollbackOn and dontRollbackOn properties, while Spring has rollbackFor and noRollbackFor, plus two additional properties: rollbackForClassName and noRollbackForClassName
2.1. Spring Transactional Annotation Configuration
As an example, let’s use and configure the Spring Transactional annotation on a simple car service:
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional(
isolation = Isolation.READ_COMMITTED,
propagation = Propagation.SUPPORTS,
readOnly = false,
timeout = 30)
public class CarService {
@Autowired
private CarRepository carRepository;
@Transactional(
rollbackFor = IllegalArgumentException.class,
noRollbackFor = EntityExistsException.class,
rollbackForClassName = "IllegalArgumentException",
noRollbackForClassName = "EntityExistsException")
public Car save(Car car) {
return carRepository.save(car);
}
}
2.3. JTA Transactional Annotation Configuration
Let’s do the same for a simple rental service using the JTA Transactional annotation:
import javax.transaction.Transactional;
@Service
@Transactional(Transactional.TxType.SUPPORTS)
public class RentalService {
@Autowired
private CarRepository carRepository;
@Transactional(
rollbackOn = IllegalArgumentException.class,
dontRollbackOn = EntityExistsException.class)
public Car rent(Car car) {
return carRepository.save(car);
}
}
3. Applicability and Interchangeability
JTA Transactional annotation applies to CDI-managed beans and classes defined as managed beans by the Java EE specification, whereas Spring’s Transactional annotation applies only to Spring beans.
It’s also worth noting that support for JTA 1.2 was introduced in Spring Framework 4.0. Thus, we can use the JTA Transactional annotation in Spring applications. However, the other way around is not possible since we can’t use Spring annotations outside the Spring context.
4. Conclusion
In this tutorial, we discussed the differences between Transactional annotations from Spring and JTA, and when we can use one or another.
As always, the code from this tutorial is available over on GitHub.