Declarative Transaction Management
-
对应用程序代码影响最小,符合非侵入式轻量级容器的原则。
-
不需要理解 AOP 概念即可使用,因为事务方面代码附带 Spring 发行版。
-
与 EJB CMT 类似,可指定方法级别的事务行为,但不受 JTA 限制,可与不同的事务环境配合使用。
-
提供声明式回滚规则,无需在 Java 代码中指定,降低了业务对象对事务基础设施的依赖性。
-
支持 AOP 自定义事务行为,但不支持跨远程调用的事务上下文传播。
大多数 Spring Framework 用户选择声明式事务管理。此选项对应用程序代码的影响最小,因此最符合非侵入轻量级容器的理想。 |
Most Spring Framework users choose declarative transaction management. This option has the least impact on application code and, hence, is most consistent with the ideals of a non-invasive lightweight container. |
Spring 框架的声明式事务管理是通过 Spring 面向方面编程 (AOP) 实现的。但是,由于事务方面代码附带 Spring 框架发行版,而且可能以样板化的方式使用,因此通常无需理解 AOP 概念就能有效使用此代码。
The Spring Framework’s declarative transaction management is made possible with Spring aspect-oriented programming (AOP). However, as the transactional aspects code comes with the Spring Framework distribution and may be used in a boilerplate fashion, AOP concepts do not generally have to be understood to make effective use of this code.
Spring 框架的声明式事务管理类似于 EJB CMT,你可以将事务行为(或没有行为)指定到各个方法级别。如有必要,可以在事务上下文中进行一次 setRollbackOnly()
调用。这两种事务管理之间的差异如下:
The Spring Framework’s declarative transaction management is similar to EJB CMT, in that
you can specify transaction behavior (or lack of it) down to the individual method level.
You can make a setRollbackOnly()
call within a transaction context, if
necessary. The differences between the two types of transaction management are:
-
Unlike EJB CMT, which is tied to JTA, the Spring Framework’s declarative transaction management works in any environment. It can work with JTA transactions or local transactions by using JDBC, JPA, or Hibernate by adjusting the configuration files.
-
You can apply the Spring Framework declarative transaction management to any class, not merely special classes such as EJBs.
-
The Spring Framework offers declarative rollback rules, a feature with no EJB equivalent. Both programmatic and declarative support for rollback rules is provided.
-
The Spring Framework lets you customize transactional behavior by using AOP. For example, you can insert custom behavior in the case of transaction rollback. You can also add arbitrary advice, along with transactional advice. With EJB CMT, you cannot influence the container’s transaction management, except with
setRollbackOnly()
. -
The Spring Framework does not support propagation of transaction contexts across remote calls, as high-end application servers do. If you need this feature, we recommend that you use EJB. However, consider carefully before using such a feature, because, normally, one does not want transactions to span remote calls.
回滚规则的概念非常重要。它们让你指定哪些异常(和可抛出对象)应导致自动回滚。你可以用声明式的方式在配置中指定这一点,而不在 Java 代码中指定。因此,尽管你仍然可以在 TransactionStatus
对象上调用 setRollbackOnly()
以回滚当前事务,但通常你可以指定一条规则,即 MyApplicationException
必须始终导致回滚。此选项的显著优点在于,业务对象不依赖于事务基础设施。例如,它们通常无需导入 Spring 事务 API 或其他 Spring API。
The concept of rollback rules is important. They let you specify which exceptions
(and throwables) should cause automatic rollback. You can specify this declaratively, in
configuration, not in Java code. So, although you can still call setRollbackOnly()
on
the TransactionStatus
object to roll back the current transaction back, most often you
can specify a rule that MyApplicationException
must always result in rollback. The
significant advantage to this option is that business objects do not depend on the
transaction infrastructure. For example, they typically do not need to import Spring
transaction APIs or other Spring APIs.
尽管 EJB 容器默认的行为会自动在系统异常(通常是运行时异常)发生时自动回滚事务,但是,EJB CMT 不会在应用程序异常(即 java.rmi.RemoteException
之外的已检查异常)发生时自动回滚事务。Spring 默认的声明式事务管理行为遵循 EJB 惯例(仅在发生未检查异常时自动回滚),但是,自定义此行为通常很有用。
Although EJB container default behavior automatically rolls back the transaction on a
system exception (usually a runtime exception), EJB CMT does not roll back the
transaction automatically on an application exception (that is, a checked exception
other than java.rmi.RemoteException
). While the Spring default behavior for
declarative transaction management follows EJB convention (roll back is automatic only
on unchecked exceptions), it is often useful to customize this behavior.