Difference between JTA and Spring @Transactional annotations
Asked Answered
T

1

10

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.

Towers answered 8/5, 2012 at 19:27 Comment(6)
Which version of JavaEE are you using?Trophic
@Trophic Java 7 is preferred, but Java 6 would work.Towers
I said JavaEE version, not JavaSE version. Your questions concern parts of the JavaEE API, which is highly version dependent.Trophic
@Trophic Oh, we're not using JavaEE. Maybe I'm missing something... We're using Tomcat 6, looking to move to Tomcat 7 in the future. Is the JTA package only available with JavaEE?Towers
JTA and JPA are part of JavaEE, as are servlets and JSP.Trophic
@Trophic Ok, so sorry for my ignorance. I talked to someone a little more knowledgeable on all this, and we are using JavaEE 6Towers
T
12

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.

Trophic answered 8/5, 2012 at 20:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.