【发布时间】:2015-06-07 10:15:31
【问题描述】:
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 模块会话 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?是你自己的例外吗?如果是,请向我们展示它的代码,否则,请说明它是哪个异常。 -
我做不到,它是一个带有编译代码的库。如何查看编译后的代码?
-
我想我可以简单地抛出一个检查异常来防止这种情况。但是什么样的检查异常呢?
-
你至少可以告诉我们异常的包名。
-
包 edu.whs.dvi;而公共类 ApplicationException extends Exception 就是类。
标签: java jakarta-ee transactions ejb