我不明白注释javax.transaction.Transactional和org.springframework.transaction.annotation.Transactional之间的实际区别是什么?

org.springframework.transaction.annotation.Transactional是javax.transaction.Transactional的扩展还是它们有完全不同的含义?什么时候应该使用它们?Spring @Transactinal在服务层和javax在DAO?

谢谢你的回答。


当前回答

声明性事务范围

Spring和JPA @Transaction注释都允许您定义给定应用程序事务的范围。

因此,如果使用@Transactional注释注释了一个服务方法,那么它将在事务上下文中运行。如果服务方法使用多个DAO或存储库,则所有读和写操作都将在同一个数据库事务中执行。

Spring @ transactional

transactional注释从Spring框架的1.2版本(大约在2005年)开始就可用了,它允许你设置以下事务属性:

isolation: the underlying database isolation level noRollbackFor and noRollbackForClassName: the list of Java Exception classes that can be triggered without triggering a transaction rollback rollbackFor and rollbackForClassName: the list of Java Exception classes that trigger a transaction rollback when being thrown propagation: the transaction propagation type given by the Propagation Enum. For instance, if the transaction context can be inherited (e.g., REQUIRED) or a new transaction context should be created (e.g., REQUIRES_NEW) or if an exception should be thrown if no transaction context is present (e.g., MANDATORY) or if an exception should be thrown if a current transaction context is found (e.g., NOT_SUPPORTED). readOnly: whether the current transaction should only read data without applying any changes. timeout: how many seconds should the transaction context be allowed to run until a timeout exception is thrown. value or transactionManager: the name of the Spring TransactionManager bean to be used when binding the transaction context.

Java EE @事务性

javax.transaction.Transactional注释是由Java EE 7规范(大约2013年)添加的。因此,Java EE注释比Spring注释晚添加了8年。

Java EE @Transactional只定义了3个属性:

dontRollbackOn: the list of Java Exception classes that can be triggered without triggering a transaction rollback rollbackOn: the list of Java Exception classes that trigger a transaction rollback when being thrown value: the propagation strategy, given by the TxType Enum. For instance, if the transaction context can be inherited (e.g., REQUIRED) or a new transaction context should be created (e.g., REQUIRES_NEW) or if an exception should be thrown if no transaction context is present (e.g., MANDATORY) or if an exception should be thrown if a current transaction context is found (e.g., NOT_SUPPORTED).

选择哪一个?

如果您正在使用Spring或Spring Boot,那么请使用Spring @Transactional注释,因为它允许您配置比Java EE @Transactional注释更多的属性。

如果您单独使用Java EE,并且将应用程序部署在Java EE应用服务器上,那么请使用Java EE @Transactional注释。

其他回答

多年前,Spring已经定义了自己的Transactional注释,以使Spring bean方法具有事务性。

Java EE 7终于做了同样的事情,现在除了EJB方法外,还允许CDI bean方法是事务性的。因此,从Java EE 7开始,它还定义了自己的Transactional注释(显然不能重用Spring注释)。

在Java EE 7应用程序中,您将使用Java EE注释。

在Spring应用程序中,您将使用Spring注释。

它们的用途是相同的:通知容器(Java EE或Spring)某个方法是事务性的。

声明性事务范围

Spring和JPA @Transaction注释都允许您定义给定应用程序事务的范围。

因此,如果使用@Transactional注释注释了一个服务方法,那么它将在事务上下文中运行。如果服务方法使用多个DAO或存储库,则所有读和写操作都将在同一个数据库事务中执行。

Spring @ transactional

transactional注释从Spring框架的1.2版本(大约在2005年)开始就可用了,它允许你设置以下事务属性:

isolation: the underlying database isolation level noRollbackFor and noRollbackForClassName: the list of Java Exception classes that can be triggered without triggering a transaction rollback rollbackFor and rollbackForClassName: the list of Java Exception classes that trigger a transaction rollback when being thrown propagation: the transaction propagation type given by the Propagation Enum. For instance, if the transaction context can be inherited (e.g., REQUIRED) or a new transaction context should be created (e.g., REQUIRES_NEW) or if an exception should be thrown if no transaction context is present (e.g., MANDATORY) or if an exception should be thrown if a current transaction context is found (e.g., NOT_SUPPORTED). readOnly: whether the current transaction should only read data without applying any changes. timeout: how many seconds should the transaction context be allowed to run until a timeout exception is thrown. value or transactionManager: the name of the Spring TransactionManager bean to be used when binding the transaction context.

Java EE @事务性

javax.transaction.Transactional注释是由Java EE 7规范(大约2013年)添加的。因此,Java EE注释比Spring注释晚添加了8年。

Java EE @Transactional只定义了3个属性:

dontRollbackOn: the list of Java Exception classes that can be triggered without triggering a transaction rollback rollbackOn: the list of Java Exception classes that trigger a transaction rollback when being thrown value: the propagation strategy, given by the TxType Enum. For instance, if the transaction context can be inherited (e.g., REQUIRED) or a new transaction context should be created (e.g., REQUIRES_NEW) or if an exception should be thrown if no transaction context is present (e.g., MANDATORY) or if an exception should be thrown if a current transaction context is found (e.g., NOT_SUPPORTED).

选择哪一个?

如果您正在使用Spring或Spring Boot,那么请使用Spring @Transactional注释,因为它允许您配置比Java EE @Transactional注释更多的属性。

如果您单独使用Java EE,并且将应用程序部署在Java EE应用服务器上,那么请使用Java EE @Transactional注释。

请小心,(此问题发生在tomcat中),

如果您的应用程序是SPRING web应用程序,并且您正在使用SPRING的事务处理机制,即@org.springframework.transaction.annotation。事务性的,那么不要将它与javax.transaction.Transactional混合。

这是Always use, @org.springframework.transaction.annotation。spring应用程序中的事务性一致性。

否则我们可能会得到这个错误,

org.springframework.orm.jpa.JpaSystemException: commit failed; nested exception is org.hibernate.TransactionException: commit failed

........

Caused by: java.sql.SQLException: Protocol violation: [0]

另一个区别是Spring如何处理@Transactional注释

org.springframework.transaction.annotation.Transactional is always taken into account javax.transaction.Transactional is 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 only javax.transaction.Transactional is in your classpath and not javax.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 provide javax.ejb.TransactionAttribute.