Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EJB3 / JPA @Transactional

Tags:

java

spring

ejb

Is there an EJB or JPA annotiation that is equivalent to Spring's @Transactional ?

like image 378
Ryan Avatar asked Jul 28 '11 17:07

Ryan


People also ask

What does @transactional do in JPA?

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.

What does the @transactional annotation mean?

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".

What is difference between javax transactional and spring transactional?

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.

What is the function of @transactional annotation at class level spring?

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.


1 Answers

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.

like image 55
joelittlejohn Avatar answered Sep 19 '22 11:09

joelittlejohn