Difference between JTA and Spring @Transactional annotations

dnc253 picture dnc253 · May 8, 2012 · Viewed 9.1k times · Source

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.

Answer

skaffman picture skaffman · May 8, 2012

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.