Choosing Between Programmatic and Declarative Transaction Management
通常,只有当您需要少量的交易操作时,程序化事务管理才是一个好主意。例如,如果您有一个 Web 应用程序,该应用程序仅为某些更新操作需要事务,那么您可能不想使用 Spring 或任何其他技术来设置事务代理。在这种情况下,使用 TransactionTemplate
可能是一种好方法。能够显式设置事务名称也是只能通过使用面向程序的事务管理方法实现的操作。
Programmatic transaction management is usually a good idea only if you have a small
number of transactional operations. For example, if you have a web application that
requires transactions only for certain update operations, you may not want to set up
transactional proxies by using Spring or any other technology. In this case, using the
TransactionTemplate
may be a good approach. Being able to set the transaction name
explicitly is also something that can be done only by using the programmatic approach
to transaction management.
另一方面,如果您的应用程序有大量事务操作,那么声明式事务管理通常是值得的。它将事务管理排除在业务逻辑之外,并且配置起来并不困难。使用 Spring 框架而不是 EJB CMT 时,声明式事务管理的配置成本大大降低。
On the other hand, if your application has numerous transactional operations, declarative transaction management is usually worthwhile. It keeps transaction management out of business logic and is not difficult to configure. When using the Spring Framework, rather than EJB CMT, the configuration cost of declarative transaction management is greatly reduced.