Is there an EJB or JPA annotiation that is equivalent to Spring's @Transactional ?
The @Transactional annotation is the metadata that specifies the semantics of the transactions on a method. We have two ways to rollback a transaction: declarative and programmatic. In the declarative approach, we annotate the methods with the @Transactional annotation.
The @Transactional annotation is metadata that specifies that an interface, class, or method must have transactional semantics; for example, "start a brand new read-only transaction when this method is invoked, suspending any existing transaction".
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.
The @Transactional annotation provides the following attributes: value and transactionManager – these attributes can be used to provide a TransactionManager reference to be used when handling the transaction for the annotated block.
The equivalent EJB3 attribute is javax.ejb.TransactionAttribute
.
Just like Spring's @Transactional
annotation, you can control the transaction 'propagation' by passing a TransactionAttributeType
to the TransactionAttribute
annotation, like:
@TransactionAttribute(NOT_SUPPORTED)
@Stateful
public class TransactionBean implements Transaction {
...
@TransactionAttribute(REQUIRES_NEW)
public void firstMethod() {...}
@TransactionAttribute(REQUIRED)
public void secondMethod() {...}
public void thirdMethod() {...}
public void fourthMethod() {...}
}
Container managed transactions are described in Part IV of the Java EE 5 Tutorial.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With