Spring事务内部结构

发布于 2024-12-22 09:22:38 字数 615 浏览 4 评论 0原文

情况如下:

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

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

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

  4. 事务也包含在 Web 请求中。

我感兴趣的是:

1.Spring 如何知道在成功事务后提交数据库更新?是否有一些关于事务管理的 Spring 实现的参考?

2.由于我们有交易的层次结构: 围绕Web请求的事务->Method1的Propagation=RequestNew的事务->Method2的Propagation=Required的事务,Spring如何进行事务管理以确保事务在正确的上下文中以正确的顺序执行?

简而言之,如果能够详细了解 Spring 如何执行事务管理的所有最详细的细节,或者获得文档的参考,而不是简单地手写以 JTA 或其他一些缩写词为中心的解释,那将是很棒的事情。

谢谢

The situation is as follows:

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

  2. 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.

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

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

What I am interested in knowing is:

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.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?

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.

Thanks

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

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

发布评论

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

评论(3

病女 2024-12-29 09:22:38

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

  1. 事务上下文是一种环境,其中一些特殊属性(数据库会话)可供应用程序运行时使用,否则这些属性将不可用。事务上下文通常用于确定事务的范围。
  2. Spring使用, AOP代理和XML元数据来实现声明式事务管理。
  3. 注释用于标记特定方法的事务传播行为。
  4. Spring 使用 拦截器机制 在方法上应用事务。

在这里,我重用了上面 @stacker 给出的示例,

MyClass{

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

    @Transactional
    void method1() {
    }

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

}

您也可以使用 xml 配置实现相同的功能。让我们认为这是流行且广泛使用的。

在部署时,

  • Spring 框架检查 xml 配置文件(著名的 applicationContext.xml),并根据配置扫描代码中的 @Transactional 注释(假设配置被提及为基于注释)。
  • 之后,它为标记为事务的方法生成 AOP 代理。简单来说,这些代理只不过是相关方法的包装。
  • 在这些包装器方法中,还会根据配置(即事务传播)生成事务顾问代码之前和之后。
  • 现在,当调用这些包装器方法时,事务顾问会在实际方法调用之前和之后出现。 。
  • 在上面的示例中用伪代码表示相同的内容

     ProxyMyClass{   
        我的班级我的班级;
        。
        。
        。
        顺序(){
         //交易顾问代码(通常开始/检查交易)
         myclass.sequence();
         //事务顾问代码(通常是回滚/提交)
        }
        。
        。
        。
        }
    

这就是 spring 管理事务的方式。不过有点过于简单化了。

现在回答您的问题,

。Spring 如何知道在成功事务后提交数据库更新?是否有一些关于进行事务管理的 Spring 实现的参考?

每当您在事务下调用方法时,您实际上调用了一个代理,该代理首先执行事务顾问程序(它将开始事务),然后调用实际的事务顾问程序。业务方法,一旦完成,另一个事务顾问程序就会执行(根据方法返回的方式,将提交或回滚事务)。

由于我们有一个事务层次结构:围绕 Web 请求的事务 -> Method1 的带有 Propagation=RequestNew 的事务 -> Method2 的带有 Propagation=Required 的事务,Spring 如何进行事务管理以确保事务在正确的上下文中以正确的顺序执行?

在事务层次结构的情况下,spring 框架会相应地生成事务顾问检查。对于您提到的示例,

  • 对于 method1 (RequestNew ),交易顾问代码(或交易建议)将始终创建一个新交易。
  • 对于方法2(必需),交易顾问代码(或交易建议)将检查现有交易,如果存在则使用相同的交易,否则创建一个新交易。

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

典型的 Spring 事务管理

希望这会有所帮助。

Lets make some basic statements.

  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.

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() {
    }

}

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

At the time of deployment

  • 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.

Now to answer your questions,

.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?

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).

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?

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

  • for method1 (RequestNew ) Transaction Advsor code (or transaction Advice) would be to create a new transaction always.
  • for method2 (Required ) Transaction Advisor code (or transaction Advice) would be check for existing transaction and use the same if it exists or else create a new transaction.

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

Typical Spring Transaction Management

Hope this helps.

明天过后 2024-12-29 09:22:38
Controller
@Transactional
public void sequence() {
  method1();
  method2();
}

@Transactional
void method1() {
}

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

默认传播是必需的(支持当前事务,如果不存在则创建一个新事务。)因此 m1 将使用在控制器中启动的事务。 m2被注释为REQUIRES_NEW(创建一个新事务,如果存在则暂停当前事务。)事务的顺序是您调用事务方法的顺序。

Controller
begin tx1
   |--------------------> m1 (uses tx1)
   |
   | begin tx2
   |--------------------> m2 (uses tx2)
   | commit tx2
commit tx1
Controller
@Transactional
public void sequence() {
  method1();
  method2();
}

@Transactional
void method1() {
}

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

The default propagation is REQUIRED (Support a current transaction, create a new one if none exists.) Therefore m1 will use the Transaction started in the Controller. m2 is annotated as REQUIRES_NEW ( Create a new transaction, suspend the current transaction if one exists.) The order of the transaction is the order you call the transactional methods.

Controller
begin tx1
   |--------------------> m1 (uses tx1)
   |
   | begin tx2
   |--------------------> m2 (uses tx2)
   | commit tx2
commit tx1
轻许诺言 2024-12-29 09:22:38

Have you read the Spring documentation? Basically AOP is used to manage the transaction. You should also read the AOP documentation. If the AOP documentation is not enough I suggest you go through the code. Stepping through the code in debug mode with break-point would be good.

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