JTA 和 Spring @Transactional 注解的区别 [英] Difference between JTA and Spring @Transactional annotations

查看:46
本文介绍了JTA 和 Spring @Transactional 注解的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始使用Spring的@Transactional注解,它为管理事务提供了很多便利.然而,在我们的代码中使用这个注解现在让我们依赖于 Spring.我知道 JPA 类型的东西有一个 javax persistence 包,我们在其中可以用 JPA 注释来标记各种代码,但由于它们都来自 javax.persistence,我们的代码不依赖于任何特定的实现或 ORM.

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.

我想我的问题是是否有针对事务性内容的类似注释.我找到了一个 javax jta 包,但我不确定是否真的有Spring 可以实现的通用注解.基本上,我只是想知道是否有任何类型的通用 javax 类注释,我们可以将它们放在方法上来管理事务功能,以便我们不依赖于 Spring.

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.

推荐答案

我认为 JavaEE 6 不像 Spring 那样提供事务划分注释.我认为最接近的类比是使用 EJB 无状态会话 bean,它们本质上是事务性的,但没有明确划分为这样(如果我错了,请有人纠正我).

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

您可以选择将代码耦合到哪个 API.使用 Spring 的 @Transactional 的优点是您独立于所使用的特定事务 API(@Transactional 可以与 JTA、JPA、Hibernate 或原始 JDBC 事务一起使用,无需更改代码).但是,当然,您与 Spring 息息相关.

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.

如果注解风格是你想要的,那么我看不到 Spring 的替代品,除非你想完全拥抱 JavaEE 并使用 EJB 3.x 风格的注解.

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.

这篇关于JTA 和 Spring @Transactional 注解的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆