Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between JTA and Spring @Transactional annotations

I've been starting to use Spring's @Transactional annotation, and it provides a lot of convenience for managing transactions. However, using this annotation in our code now makes us dependent on Spring. I know with JPA type stuff there is a javax persistence package where we can mark up the code with all sorts if JPA annotations, but since they all come from javax.persistence, our code isn't dependent on any specific implementation or ORM.

I guess my question is if there are any similar annotations for transactional stuff. I found a javax jta package, but I'm not sure there's really any generic annotations that Spring could implement. Basically I'm just wondering if there's any kind of generic javax-like annotations that we can put on methods to manage transactional functionality so that we aren't dependent on Spring.

like image 701
dnc253 Avatar asked May 08 '12 19:05

dnc253


People also ask

What does @transactional annotation do in Spring?

The @Transactional annotation makes use of the attributes rollbackFor or rollbackForClassName to rollback the transactions, and the attributes noRollbackFor or noRollbackForClassName to avoid rollback on listed exceptions. The default rollback behavior in the declarative approach will rollback on runtime exceptions.

When should we use @transactional annotation?

The @Transactional annotation belongs to the Service layer because it is the Service layer's responsibility to define the transaction boundaries.

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 the use of @transactional annotation in Java?

Transactional annotation provides the application the ability to declaratively control transaction boundaries on CDI managed beans, as well as classes defined as managed beans by the Java EE specification, at both the class and method level where method level annotations override those at the class level.


1 Answers

I don't think JavaEE 6 offers transaction demarcation annotations like Spring does. The closest analogue, I think, is the use of EJB stateless session beans, which are transactional by nature, but not explicitly demarcated as such (someone please correct me if I'm wrong).

It's your call as to which API(s) you choose to couple your code to. The advantage of using Spring's @Transactional is that you're independent of the specific transaction API being used (@Transactional can work with JTA, JPA, Hibernate or raw JDBC transactions, without changing the code). But, of course, you're tied to Spring.

If the annotation style is what you want, then I see no alternative to Spring, unless you want to embrace JavaEE fully and use EJB 3.x-style annotations.

like image 85
skaffman Avatar answered Oct 05 '22 22:10

skaffman