gpt4 book ai didi

java - 防止事务标记为回滚

转载 作者:行者123 更新时间:2023-11-30 08:08:55 26 4
gpt4 key购买 nike

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 模块 session bean 调用指定信用卡方法。这是我的 EJB 模块端的方法:

@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.");
}
}
}

我遇到的问题是,当我输入无效内容时,我的应用程序异常将被抛出。因此,现在事务将被标记为回滚,并且我无法再次重复此方法,因为事务无效(javax.ejb.EJBException:javax.transaction.InvalidTransactionException)。

如果输入错误后,我的交易没有被标记为回滚,我该怎么办?提前致谢。

最佳答案

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

@ApplicationException(rollback=false)
public class MyApplicationException extends ApplicationException {
...
}

...
throw new MyApplicationException(); //does not rollback the transaction

关于java - 防止事务标记为回滚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30692370/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com