简体   繁体   English

春季交易+休眠

[英]Transaction in Spring + hibernate

We are using transaction management through declarative approach, we have specify that all the method which is start with update* must follow the transaction. 我们通过声明式方法使用事务管理,我们指定所有以update *开头的方法都必须遵循事务。

Suppose we have a transaction method updatePayroll() inside this method we are calling four another method, but our requirement is to rollback only in case of first two method get failed else no rollback is required. 假设我们在该方法内部有一个事务处理方法updatePayroll(),我们正在调用另外四个方法,但是我们的要求是仅在前两个方法失败的情况下进行回滚,否则不需要回滚。 I don't know how to implement this as my updatePayroll() is transacted method so how it is possible to tell the spring that we need only two method's of updatePayroll() should be transacted. 我不知道如何实现此方法,因为我的updatePayroll()是事务处理的方法,因此如何告诉Spring我们只需要事务updatePayroll()的两个方法即可。

code: 码:

public void updatePayroll()
{
// below two methods of updateParyroll is required transaction where updatePayroll is transacted method through declarative approach.
updateLWPEmployees();

processSalary();

// below methods does not require transaction.
printSalarySlip();

sendEmail();
}

Thanks in advance. 提前致谢。

if you want to avoid changes to the transaction management configuration, then 如果要避免更改事务管理配置,则

  1. you can create a dummy class extending RuntimeException , lets call it NoRollBackException , now you need to configure the trasnaction manager to not rollback in case of the NoRollBackException , later you put the two methods printSalarySlip() and sendEmail() in a try catch block and throw the NoRollBackException . 你可以创建延伸的哑类RuntimeException ,让我们把它叫做NoRollBackException ,现在你需要配置trasnaction经理不在的情况下回滚NoRollBackException ,以后你把两种方法printSalarySlip()和sendEmail()在try catch块和抛出NoRollBackException

or 要么

  1. (a more simpler approach) you could call the two methods ( printSalarySlip() and sendEmail() ) in a seperate function after the call to updatePayroll suceedes. (一种更简单的方法),您可以在调用updatePayroll后调用一个单独的函数中的两个方法( printSalarySlip() and sendEmail() )。

or 要么

  1. you can catcgh the exception thrown by the two methods and not allow the exception to be propogated. 您可以处理这两种方法引发的异常,并且不允许传播该异常。

all the above methods does exactly what you are looking for. 以上所有方法完全可以满足您的需求。

Why not write a transacted method including the first two methods? 为什么不编写包括前两种方法的事务处理方法? And then let the method updatePayroll() without transaction include this method? 然后让没有事务的方法updatePayroll()包含此方法吗?

Before saying any technical thing, i'll outline the fact that transactions aren't pure technical concept, i hope that this need isn't because you only want to avoid some technical dificulties you faced. 在讲任何技术问题之前,我将概述交易不是纯粹的技术概念这一事实,我希望这种需求不是因为您只想避免遇到的一些技术难题。 We need to keep as possible unchanged the semantic of transaction rollback behavior following the trigering of an exception: ie when a technical problem(RuntimeException) is encountred you better let the transaction rollback(and this is the default behavior) because you didn't expect such a scenario, if you find out that it's technically incosistent, then probably this processing is not logically part of the transaction. 触发异常后,我们需要尽可能保持事务回滚行为的语义不变:即,遇到技术问题(RuntimeException)时,最好让事务回滚(这是默认行为),因为您没有想到在这种情况下,如果您发现它在技术上并不重要,那么从逻辑上讲,此处理就不是事务的一部分。 Otherwise, if it's your exception that is thrown(a business exception, planed during design) then you have total control to rollback or not as explained in the first answear. 否则,如果引发的是您的异常(业务异常,在设计期间进行了计划),则您可以完全控制是否回滚,如第一个问题所述。

@woodpecker: the solution you proposed is not the same technically speaking when it comes to isolation, especially when we're trying to avoid dirty and repeatable reading @woodpecker:您提出的解决方案在技术上与隔离无关,特别是在我们试图避免脏乱且可重复的阅读时

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM