有没有办法强制事务性回滚而不会遇到异常? [英] is there a way to force a transactional rollback without encountering an exception?

查看:93
本文介绍了有没有办法强制事务性回滚而不会遇到异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种方法可以处理一堆事情;在他们之中做一些插入和更新。它是这样声明的...

$ p $ @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,readOnly = false)
public int saveAll(){
// do stuff;
}

它的工作原理与我的理解完全一样,并且我没有任何问题。然而,当我想要强制回滚时,尽管没有出现异常情况,但有些情况下......目前,当我遇到正确的条件时,我强制执行一个例外情况,但这很丑陋,我不喜欢它。

我能否以某种方式主动调用回滚?

解决方案

调用 setRollbackOnly( )在 SessionContext 上,如果您在EJB中。



您可以注入 SessionContext 就像这样:

  public MyClass {
@Resource
private SessionContext sessionContext;
$ b @Transactional(propagation = Propagation.REQUIRED,
isolation = Isolation.DEFAULT,
readOnly = false)
public int saveAll(){
/ /做东西;
if(oops == true){
sessionContext.setRollbackOnly();
return;


code $
$ b $ p $ setRollbackOnly() code>是 EJBContext 的成员。 SessionContext 扩展 EJBContext
http://java.sun.com/j2ee/1.4/docs/api/javax/ejb/SessionContext.html 请注意,它仅在会话EJB中可用。
$ b @Resource 是标准的Java EE注释,因此您应该检查您在Eclipse中的设置。以下是如何使用示例 > @Resource



我怀疑这可能不是您的解决方案,因为您似乎可能没有使用EJB - 解释为什么Eclipse没有找到 @Resource



如果是这种情况,那么您将需要与事务交互直接 - 查看交易模板。


I have a method that does a bunch of things; amongst them doing a number of inserts and updates. It's declared thusly...

@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, readOnly = false)
public int saveAll(){
 //do stuff;
}

It works exactly as it is supposed to and I have no problems with it. There are situations however when I want to force the rollback in spite of there not being an exception... at the moment, I'm forcing an exception when I encounter the right conditions, but it's ugly and I don't like it.

Can I actively call the rollback somehow? The exception calls it... I'm thinking maybe I can too.

解决方案

Call setRollbackOnly() on the SessionContext if you're in an EJB.

You can inject SessionContext like so:

public MyClass {
    @Resource
    private SessionContext sessionContext;

    @Transactional(propagation = Propagation.REQUIRED, 
                   isolation = Isolation.DEFAULT, 
                   readOnly = false)
    public int saveAll(){
        //do stuff;
        if(oops == true) {
             sessionContext.setRollbackOnly();
             return;
        }
    }

setRollbackOnly() is a member of EJBContext. SessionContext extends EJBContext: http://java.sun.com/j2ee/1.4/docs/api/javax/ejb/SessionContext.html Note it's only available in session EJBs.

@Resource is a standard Java EE annotation, so you should probably check your setup in Eclipse. Here's an example of how to inject the SessionContext using @Resource.

I suspect that this is probably not your solution, since it seems like you may not be working with EJBs -- explaining why Eclipse is not finding @Resource.

If that's the case, then you will need to interact with the transaction directly -- see transaction template.

这篇关于有没有办法强制事务性回滚而不会遇到异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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