javax.transaction.Transactional vs org.springframework.transaction.annotation.Transactional
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26387399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javax.transaction.Transactional vs org.springframework.transaction.annotation.Transactional
提问by stamis
I don't understand what is the actual difference between annotations javax.transaction.Transactional
and org.springframework.transaction.annotation.Transactional
?
我不明白 annotationsjavax.transaction.Transactional
和org.springframework.transaction.annotation.Transactional
?之间的实际区别是什么?
Is org.springframework.transaction.annotation.Transactional
an extension of javax.transaction.Transactional
or they have totally different meaning? When should each of them be used? Spring @Transactinal
in service layer and javaxin DAO?
是它们org.springframework.transaction.annotation.Transactional
的延伸javax.transaction.Transactional
还是它们的含义完全不同?什么时候应该使用它们中的每一个?@Transactinal
服务层中的Spring和DAO中的javax?
Thanks for answering.
谢谢回答。
采纳答案by JB Nizet
Spring has defined its own Transactional annotation to make Spring bean methods transactional, years ago.
几年前,Spring 已经定义了自己的 Transactional 注释,以使 Spring bean 方法具有事务性。
Java EE 7 has finally done the same thing and now allows CDI bean methods to be transactional, in addition to EJB methods. So since Java EE 7, it also defines its own Transactional annotation (it obviously can't reuse the Spring one).
Java EE 7 终于做了同样的事情,现在除了 EJB 方法之外,还允许 CDI bean 方法是事务性的。所以从Java EE 7开始,它也定义了自己的Transactional注解(显然不能复用Spring的)。
In a Java EE 7 application, you'll use the Java EE annotation.
在 Java EE 7 应用程序中,您将使用 Java EE 注释。
In a Spring application, you'll use the Spring annotation.
在 Spring 应用程序中,您将使用 Spring 注释。
Their use is the same: informing the container (Java EE or Spring) that a method is transactional.
它们的用途是一样的:通知容器(Java EE 或 Spring)一个方法是事务性的。
回答by Lyju I Edwinson
Please be careful, (this issue happened in tomcat),
请小心,(这个问题发生在tomcat中),
If your application is SPRING web application and you are using Spring's transaction handling mechanism that is @org.springframework.transaction.annotation.Transactional
, then don't mix it with javax.transaction.Transactional.
如果您的应用程序是 SPRING Web 应用程序并且您使用的是 Spring 的事务处理机制 @org.springframework.transaction.annotation.Transactional
,那么不要将它与 javax.transaction.Transactional 混合使用。
That is Always use, @org.springframework.transaction.annotation.Transactional
in a spring application consistently.
那是始终使用, @org.springframework.transaction.annotation.Transactional
在 spring 应用程序中始终如一。
Otherwise we may end up with this error,
否则我们可能会遇到这个错误,
org.springframework.orm.jpa.JpaSystemException: commit failed; nested exception is org.hibernate.TransactionException: commit failed
........
Caused by: java.sql.SQLException: Protocol violation: [0]
回答by Jidehem
Another difference is how Spring handles the @Transactional annotations
另一个区别是 Spring 如何处理 @Transactional 注释
- org.springframework.transaction.annotation.Transactionalis always taken into account
- javax.transaction.Transactionalis taken into account only when EJB3 transactions are present. EJB3 transactions' presence is done by checking if class
javax.ejb.TransactionAttribute
is available in the classpath (from version 2.5.3 to 3.2.5). Thus you can end up with your annotations not being taken into account if onlyjavax.transaction.Transactional
is in your classpath and notjavax.ejb.TransactionAttribute
. This can be the case if you're working with Hibernate: hibernate-core (4.3.7.Final) depends on jboss-transaction-api_1.2_spec (1.0.0.Final), which doesn't providejavax.ejb.TransactionAttribute
.
- org.springframework.transaction.annotation.Transactional总是被考虑在内
- 仅当存在 EJB3 事务时才考虑javax.transaction.Transactional。EJB3 事务的存在是通过检查类
javax.ejb.TransactionAttribute
路径(从版本 2.5.3 到 3.2.5)中是否可用来完成的。因此,如果仅javax.transaction.Transactional
在类路径中而不是javax.ejb.TransactionAttribute
. 如果您使用 Hibernate,可能会出现这种情况:hibernate-core (4.3.7.Final) 依赖于 jboss-transaction-api_1.2_spec (1.0.0.Final),它不提供javax.ejb.TransactionAttribute
.