yemerra
yemerra

Reputation: 1342

Preventing the transaction to become marked rollback

public void specifyCreditCard(String kontonr, String vorname, String name) throws ApplicationException {
    try {
            bp.specifyCreditCard(kontonr, vorname, name);
            ApplicationExceptionInspector.checkForApplicationException();
    } catch (ApplicationException ae) {
        throw ae;
    }
}

The method above is on my client modules side. It is bean managed and I want to call the methode specifyCreditCard from a EJB modules session bean that is container managed. This is the method on my EJB modules side:

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

The problem I have is, that my appication exceptions will be thrown when I type in something invalid. So now the transaction will be marked rollback and I cannot repeat this method again, since the transaction is invalid (javax.ejb.EJBException: javax.transaction.InvalidTransactionException).

What should I do, that after a wrong input, my transaction is not marked as rollback? Thanks in advance.

Upvotes: 1

Views: 1216

Answers (1)

Vitaliy Polchuk
Vitaliy Polchuk

Reputation: 1898

It's necessary to create custom exception with the @ApplicationException annotation and throw it instead of ApplicationException:

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

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

Upvotes: 1

Related Questions