Spring事务内部结构
情况如下:
Method1 中有四种数据库更新方法。 Method1使用Spring事务管理语义进行注释。
Method2 中有一个数据库读取方法,在 Method1 执行完所有数据库更新后调用该方法。 Method2 也使用 Spring 事务语义进行注释。
有一个 Web 请求进来,控制器拦截该请求并调用 method1,然后调用 method2。
有事务也包含在 Web 请求中。
我感兴趣的是:
1.Spring 如何知道在成功事务后提交数据库更新?是否有一些关于事务管理的 Spring 实现的参考?
2.由于我们有交易的层次结构: 围绕Web请求的事务->Method1的Propagation=RequestNew的事务->Method2的Propagation=Required的事务,Spring如何进行事务管理以确保事务在正确的上下文中以正确的顺序执行?
简而言之,如果能够详细了解 Spring 如何执行事务管理的所有最详细的细节,或者获得文档的参考,而不是简单地手写以 JTA 或其他一些缩写词为中心的解释,那将是很棒的事情。
谢谢
The situation is as follows:
Method1 has four database update methods in it. The Method1 is annotated using the Spring transaction management semantics.
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.
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.
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
让我们做一些基本的陈述。
在这里,我重用了上面 @stacker 给出的示例,
您也可以使用 xml 配置实现相同的功能。让我们认为这是流行且广泛使用的。
在部署时,
applicationContext.xml
),并根据配置扫描代码中的@Transactional
注释(假设配置被提及为基于注释)。在上面的示例中用伪代码表示相同的内容
这就是 spring 管理事务的方式。不过有点过于简单化了。
现在回答您的问题,
。Spring 如何知道在成功事务后提交数据库更新?是否有一些关于进行事务管理的 Spring 实现的参考?
每当您在事务下调用方法时,您实际上调用了一个代理,该代理首先执行事务顾问程序(它将开始事务),然后调用实际的事务顾问程序。业务方法,一旦完成,另一个事务顾问程序就会执行(根据方法返回的方式,将提交或回滚事务)。
由于我们有一个事务层次结构:围绕 Web 请求的事务 -> Method1 的带有 Propagation=RequestNew 的事务 -> Method2 的带有 Propagation=Required 的事务,Spring 如何进行事务管理以确保事务在正确的上下文中以正确的顺序执行?
在事务层次结构的情况下,spring 框架会相应地生成事务顾问检查。对于您提到的示例,
弹簧上有一个图像文档页面很好地总结了这些方面。
希望这会有所帮助。
Lets make some basic statements.
Here I am reusing the example give by @stacker above
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
applicationContext.xml
) and depending on the configuration, scans the code for@Transactional
annotation (assuming that the configuration is mentioned as annotation based).Representing the same in pseudo code for the example above
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,
There is an image on the spring documentation page which very nicely summarizes these aspects.
Hope this helps.
默认传播是必需的(支持当前事务,如果不存在则创建一个新事务。)因此 m1 将使用在控制器中启动的事务。 m2被注释为REQUIRES_NEW(创建一个新事务,如果存在则暂停当前事务。)事务的顺序是您调用事务方法的顺序。
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.
你读过 Spring 文档?基本上AOP是用来管理事务的。您还应该阅读 AOP 文档。如果 AOP 文档还不够,我建议你仔细阅读代码。在调试模式下使用断点单步执行代码会很好。
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.