首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >防止事务被标记为回滚

防止事务被标记为回滚
EN

Stack Overflow用户
提问于 2015-06-07 18:15:32
回答 1查看 933关注 0票数 1
代码语言:javascript
运行
复制
public void specifyCreditCard(String kontonr, String vorname, String name) throws ApplicationException {
    try {
            bp.specifyCreditCard(kontonr, vorname, name);
            ApplicationExceptionInspector.checkForApplicationException();
    } catch (ApplicationException ae) {
        throw ae;
    }
}

上面的方法是在我的客户端模块上的。它是bean托管的,我想从容器托管的EJB modules会话bean中调用方法specifyCreditCard。这是我的EJB模块端的方法:

代码语言:javascript
运行
复制
@Override
@TransactionAttribute(TransactionAttributeType.MANDATORY)
public void specifyCreditCard(String kontonr, String name, String vorname) throws ApplicationException {
    long gesamtkosten = 0;
    for (Bestellposition bp : (ArrayList<Bestellposition>) bestellung.getPositionen()) {
        gesamtkosten = gesamtkosten + (bp.getPreis() * bp.getAnzahl());
    }
    KreditkartenkontoDTO gesuchtesKonto = kb.getKreditkartenkonto(kontonr);
    SimpleDateFormat dt = new SimpleDateFormat("yyyyy-mm-dd");
    if (gesuchtesKonto == null) {
        throw new ApplicationException("", "Eingabe der Kontonummer war fehlerhaft.");
    } else {
        if (!gesuchtesKonto.getInhaberName().equals(name)) {
            throw new ApplicationException("", "Der Nachname stimmt nicht überein.");
        }
        if (!gesuchtesKonto.getInhaberVorname().equals(vorname)) {
            throw new ApplicationException("", "Der Vorname stimmt nicht überein.");
        }
        if (!new Date().before(gesuchtesKonto.getAblaufDatum())) {
            throw new ApplicationException("", "Konto ist ungültig.");
        }
        long belastung = gesuchtesKonto.getBelastung() + gesamtkosten;

        if (belastung < gesuchtesKonto.getLimit()) {
            gesuchtesKonto.setBelastung(belastung);
            kb.updateBelastung(gesuchtesKonto);
        } else {
            throw new ApplicationException("", "Ihr Kontostand ist für diese Bestellung im Wert von " + gesamtkosten + " € zu niedrig.");
        }
    }
}

我的问题是,当我输入一些无效的东西时,我的应用程序异常将被抛出。因此,现在该事务将被标记为rollback,并且我不能重复此方法,因为该事务是无效的( javax.transaction.InvalidTransactionException).:javax.ejb.EJBException

输入错误后,我的事务没有被标记为回滚,我该怎么办?提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2015-06-08 22:54:52

有必要使用@ApplicationException注释创建自定义异常,并抛出它而不是ApplicationException:

代码语言:javascript
运行
复制
@ApplicationException(rollback=false)
public class MyApplicationException extends ApplicationException {
    ...
}

...
throw new MyApplicationException(); //does not rollback the transaction
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30692370

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档