Springs 事务管理 - 了解 Spring 参考,全局/本地,编程/声明式 - 两个问题

发布于 2024-11-06 06:09:31 字数 1407 浏览 0 评论 0 原文

我使用 Spring Framework 3.0.5 和 Hibernate Framework,现在开始使用 Springs Transactionmanagement。我有一些问题,只是想了解 Springs Transactionmanagement 的工作原理。

1)

我在 Spring 参考文献中读到了这些内容:

a) 跨不同事务 API(例如 Java 事务 API (JTA)、JDBC、Hibernate、Java 持久性 API (JPA) 和 Java 数据对象 (JDO))的一致编程模型。

b) Spring解决了全局事务和本地事务的缺点。它使应用程序开发人员能够在任何环境中使用一致的编程模型。您编写一次代码,它就可以从不同环境中的不同事务管理策略中受益。

c) 使用 EJB CMT 或 JTA 的唯一替代方案是使用本地事务(例如 JDBC 连接上的事务)编写代码的日子已经一去不复返了,如果您需要该代码在全局容器中运行,那么您将面临大量返工 -托管交易。使用 Spring 框架,只需更改配置文件中的部分 bean 定义,而不需要更改代码。

a) 我了解我可以在 Spring 中使用这些 API,无需更改代码

来自 b) 我了解我可以使用全局或本地事务 *无需更改代码

来自c) 我了解,在不同 API 和全局/本地事务之间切换时,我需要更改代码

现在我想知道什么是正确的?

=> 我需要更改代码吗?当在不同的API之间切换时?什么时候在本地和全局事务之间切换? (或者它可能依赖于程序化和声明式事务管理?)

2)

我还有一个额外的问题:我真的想知道程序化事务管理的用途是什么?在我读到建议使用声明式事务管理的地方,

我也在 spring 参考中读到了这一点:

d) 通过编程式事务管理,开发人员可以使用 Spring 框架事务抽象,该抽象可以在任何底层事务基础设施上运行。使用首选的声明式模型,开发人员通常编写很少或不编写与事务管理相关的代码,因此不依赖于 Spring 框架事务 API 或任何其他事务 API。

从 d) 我了解到:通过程序化事务管理,我可以使用任何底层事务基础设施......这意味着什么?上面提到的不同 API?

并且:使用声明式,我不依赖于任何 api

=> 这不是一样的吗?当我可以使用任何底层api时,我不依赖任何api。我不太明白这一点。

区别在哪里?我只知道声明式事务管理更加轻量级,不用自己去启动事务,捕获异常并处理等等。但是程序化事务管理有什么用呢?

谢谢您的回答! :-)

Im working with the Spring Framework 3.0.5 and the Hibernate Framework and Im starting to use now Springs Transactionmanagement. I have some questions, just to understand how Springs Transactionmanagement works.

1)

I read this things in the Spring reference:

a) Consistent programming model across different transaction APIs such as Java Transaction API (JTA), JDBC, Hibernate, Java Persistence API (JPA), and Java Data Objects (JDO).

b) Spring resolves the disadvantages of global and local transactions. It enables application developers to use a consistent programming model in any environment. You write your code once, and it can benefit from different transaction management strategies in different environments.

c) Gone are the days when the only alternative to using EJB CMT or JTA was to write code with local transactions such as those on JDBC connections, and face a hefty rework if you need that code to run within global, container-managed transactions. With the Spring Framework, only some of the bean definitions in your configuration file, rather than your code, need to change.

From a) I understand that I can use those APIs with Spring without changing the code

From b) I understand that I can use global or local transactions *without changing the code

From c) I understand that while switching between different APIs and global/local transactions I need to change the code

Now I wonder what is correct?

=> Do I need to change the code? When switching between different APIs? When switching between local and global transactions? (Or does it maybe depend on prorgammatic and declarative transaction management?)

2)

I also got an additional question: I really wonder what the use of programmatic transaction management is? Everywhere I read that declarative transactionmanagement is recommended

I read this in spring reference too:

d) With programmatic transaction management, developers work with the Spring Framework transaction abstraction, which can run over any underlying transaction infrastructure. With the preferred declarative model, developers typically write little or no code related to transaction management, and hence do not depend on the Spring Framework transaction API, or any other transaction API.

From d) I understand: with programmatic transaction management I can use any underlying transaction infrastructure... which means what? the different APIs mentioned above?

and: with declarative I do not depend on any api

=> isnt this the same? when I can use any underlying api, I do not depend on any api. I do not really understand this.

where is the difference? I only know that the declarative transaction management is more lightweight, that I have not to start the transaction by my self and catch the exception and handle it and so on. But what is the use of programmatic transaction management then?

Thank you for answering! :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一场信仰旅途 2024-11-13 06:09:31
  1. 你想得有点多了。 Spring API 提供了一个抽象事务模型,无论您使用哪种底层事务技术,该模型都具有相同的 API 和语义。为了从一种技术切换到另一种技术,您通常必须更改 Spring 配置,但我们的想法是您永远不需要更改业务逻辑。因此,无论您使用本地、VM 内 JDBC 事务还是完全分布式、两阶段提交 XA JPA 样式事务,Spring 代码中的 API 使用情况都是相同的。只是配置发生了变化。

  2. 声明式事务管理和编程式事务管理之间的区别在于,对于前者,您可以使用注释或 XML 配置来说明哪些代码位应该是事务性的。通过编程风格,您可以使用方法调用将事务逻辑专门封装到 Spring API 中。请注意,如果您使用声明式风格,那么 Spring 会将您的代码包装在使用编程风格的生成逻辑中。后者只是前者的更明确和低级的版本。它为您提供了更多控制权,但更加冗长。

  1. You're over-thinking this a bit. The Spring API provides an abstract transaction model that has the same API and semantics regardless of which underlying transaction technology you use. In order to switch from one technology to another, you generally have to alter your Spring config, but the idea is that you never needs to to alter your business logic. So whether you're using local, in-VM JDBC transactions or fully distributed, two-phase-commit XA JPA-style transactions, the API usage within your Spring code is the same. Only the configuration changes.

  2. The difference between declarative and programmatic transaction management is that with the former, you use annotations or XML config to say which bits of code are supposed to be transactional. With programmatic style, you specifically enclose transactional logic using method calls into the Spring API. Note that if you use the declarative style, then Spring will wrap your code in generated logic which uses the programmatic style. The latter is simply a more explicit and low-level version of the former. It gives you more control, but it's more verbose.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文