grails:维护从服务到控制器的事务上下文
在 grails Service 类中有一个设置了回滚标志的事务:
TransactionAspectSupport
.currentTransactionInfo()
.transactionStatus
.setRollbackOnly()
当我们返回到 Controller 时会发生什么情况:
org.springframework.transaction.UnexpectedRollbackException
我们必须在 Controller 中捕获该异常(但不能在任何 Service 类中捕获)。此代码是从以前的解决方案重构的,其中所有逻辑都直接发生在控制器中。任何关于在方法返回时发生的情况会引发此异常的建议,前提是:
static transactional = true
已在所有类上设置。猜测正在发生一些微妙的控制器与服务魔法 - 有人知道吗?目前,只是捕获异常作为解决方法,但这会丢失本来会返回的 TransactionStatus 对象。
任何想法非常感谢
Have a transaction in a grails Service class on which a rollback flag is set:
TransactionAspectSupport
.currentTransactionInfo()
.transactionStatus
.setRollbackOnly()
what happens is that when we return to the Controller an exception:
org.springframework.transaction.UnexpectedRollbackException
which we have to catch in the Controller (but not in any of the Service classes). This code is being refactored from the previous solution where all the logic happened direct in the Controller. Any advise on what happens that trips this exception to be thrown when the method returns, given that:
static transactional = true
has been set on all the classes. Guessing theres some subtle Controller verses Service magic happening - does anyone know about this? For now just catching the exception as a workaround, but this loses the TransactionStatus object that otherwise would have been returned.
Any thoughts much appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Grails 中的事务管理非常丑陋(对我来说)。
所以我提供 Spring 声明式事务:
第 9 章 事务管理
它们在 grails 服务中完美运行。
返回到setRollbackOnly()。这个方法并不简单......当您在内部事务中设置 RollBack=true 时,您已触发将外部事务回滚到 sou,但您遇到了异常。
我前段时间遇到过类似的问题 - 这里有一些有用的信息,可以找到适合您的最佳解决方案:
Transaction management in Grails is pretty ugly (for me).
So i'm proffering Spring declarative transactions:
Chapter 9. Transaction management
They works perfectly in grails services.
Returning back to setRollbackOnly(). This method is not simple... While you have set RollBack=true in your inner transaction you have triggered to rollback your outer transaction to sou you are getting exception.
I've similar problem some time ago - here is useful info to find best solution suiting for you: