Spring事务内部 [英] Spring transaction internals

查看:157
本文介绍了Spring事务内部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况如下:


  1. Method1中有四种数据库更新方法。 Method1使用Spring事务管理语义进行注释。

  1. Method1 has four database update methods in it. The Method1 is annotated using the Spring transaction management semantics.

Method2中有一个数据库读取方法,在Method1完成所有数据库更新后调用它。 Method2也使用Spring事务语义进行注释。

Method2 has a database read method in it and it is invoked after Method1 has finished executing all its database updates. Method2 is also annotated using the Spring transaction semantics.

有一个Web请求进入,控制器拦截请求并调用method1然后调用method2。

There's a web request that comes in, the controller intercepts the request and invokes method1 and then method2.

交易也包含在网络请求中。

A transaction is wrapped around the web-request as well.

我感兴趣的是:

1. Spring如何知道在成功交易时提交数据库更新?是否有一些引用进行事务管理的Spring实现?

1.How does Spring know to commit the database updates upon a successful transaction? Is there some reference to the Spring implementation that does the transaction management?

2.由于我们有一个事务层次结构:
围绕web请求的事务 - >传播的事务= Method1的RequestNew>传播的事务=方法2需要,Spring如何进行事务管理以确保事务在正确的上下文中以正确的顺序执行?

2.Since we have a hierarchy of transactions: Transaction around the web-request->Transaction with Propagation=RequestNew for Method1->Transaction with Propagation=Required for Method2, how does Spring do the transaction management to ensure the transactions are executed within the proper context with the right order?

简而言之,通过游戏帐户了解Spring如何在其所有最复杂的细节中执行事务管理或参考文档不仅仅是手动挥动以JTA为中心的解释,其他一些缩写。

In short, it will be great to get a play by play account of how Spring performs the transaction management in all its grittiest details or a reference to documentation that doesn't simply hand-wave an explanation centered around JTA or some other acronym.

谢谢

推荐答案

让我们做一些基本的陈述。

Lets make some basic statements.


  1. 事务上下文是一种环境,其中一些特殊属性(数据库会话)可供应用程序运行时使用明智的不可用。事务上下文通常用于确定事务的范围。

  2. Spring使用, AOP Proxies 和XML元数据,以实现声明式事务管理。

  3. 注释用于标记特定方法的事务传播行为。

  4. Spring使用拦截机制来应用交易方法。

  1. A transactional context is an environment where some special properties (database session) are made available to the application runtime which are otherwise not available. A Transaction Context is generally used to scope a transaction.
  2. Spring uses, AOP Proxies and XML metadata to achieve a Declarative transaction management.
  3. Annotations are used to mark the Transaction Propagation behavior of a particular method.
  4. Spring uses Interceptor Mechanism to apply the transaction over the methods.

这里我重复使用@stacker上面给出的示例

Here I am reusing the example give by @stacker above

MyClass{

    @Transactional
    public void sequence() {
      method1();
      method2();
    }

    @Transactional
    void method1() {
    }

    @Transactional(propagation=Propagation.REQUIRES_NEW)
    void method2() {
    }

}

你可以也使用xml配置实现相同的功能。让我们把它作为它的流行和广泛使用。

You can also achieve the same functionality using xml configuration as well. Lets take this as its popular and widely used.

在部署时


  • Spring框架检查xml配置文件(着名的 applicationContext.xml ),并根据配置扫描代码 @Transactional 注释(假设配置被提及为基于注释)。

  • 在此之后,为标记为事务的方法生成 AOP代理。简单来说,这些代理只是相关方法的包装。

  • 在这些包装器方法中,在生成 Transaction Advisor 代码之前和之后依赖于配置(即事务传播)。

  • 现在,当调用这些包装器方法时, Transaction Advisor 会在实际方法调用之前和之后进入图片。 。

  • 在上面示例的伪代码中表示相同

  • Spring framework checks the xml configuration files (famed applicationContext.xml) and depending on the configuration, scans the code for @Transactional annotation (assuming that the configuration is mentioned as annotation based).
  • After this, it generates AOP proxies for methods marked for transaction. In simple terms, these proxies are nothing but wrapper around the concerned methods.
  • Within these wrapper methods, before and after a Transaction Advisor code is also generated depending on the configuration (namely the transaction propagation).
  • Now when these wrapper methods are invoked Transaction Advisor comes into picture before and after the actual method call. .
  • Representing the same in pseudo code for the example above

  ProxyMyClass{   
    MyClass myclass;
    .
    .
    .
    sequence(){
     //Transaction Advisor code (Typically begin/check for transaction)
     myclass.sequence();
     //Transaction Advisor code(Typically rollback/commit)
    }
    .
    .
    .
    }


这就是春天的样子经理人的交易。虽然稍微过于简单化了。

This is how spring managers the transaction. A slight oversimplification though.

现在回答你的问题,

。Spring怎么知道提交数据库更新一次成功的交易?是否有一些对执行事务管理的Spring实现的引用?

每当你在事务下调用一个方法时,你实际上调用的是一个首先执行事务顾问(将开始事务),然后调用实际的业务方法,一旦完成,另一个事务顾问执行(这取决于返回的方法,将提交或回滚事务)。

Whenever you call a method under transaction, you actually call a proxy which first executes the transaction advisor (which will begin the transaction), then you call the actual business method, once that completes, another transaction advisor executes (which depending on way method returned, will commit or rollback transaction).

由于我们有一个事务层次结构:围绕web-request-> Transaction with Propagation的事务= RequestNew for Method1-> Transaction with Propagation = Method2是必需的,Spring如何进行事务管理确保事务在正确的上下文中以正确的顺序执行?

如果是事务层次结构,spring框架会相应地生成Transaction Advisor检查。对于您提到的示例,

In case of transaction hierarchy, the spring framework generates the Transaction Advisor checks accordingly. For the example you mentioned,


  • for method1(RequestNew)事务Advsor代码(或事务建议)将始终创建新事务。

  • for method2(必需)Transaction Advisor代码(或事务建议)将检查现有事务,如果存在则使用相同的事务,否则创建新事务。

有一个弹簧文档页面上的图像,非常好地总结了这些方面。

There is an image on the spring documentation page which very nicely summarizes these aspects.

希望这有帮助。

这篇关于Spring事务内部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆