SpooXter
SpooXter

Reputation: 119

Silently roll transaction back in Payara 4.1

I have an EJB with container-managed transactions. I have a method in it (used as a REST call) that calls something from another EJB I've injected using JNDI (not sure if that matters) and it returns an Exception that extends RuntimeException (so it causes a transaction rollback), which translates as a 404 response through an ExceptionMapper.

I want that exception to be what returns from my REST call and I don't mind it being in the logs at all, but I do not want my log to be spammed with the EJBExceptionRolledBackException stacktrace that it causes (the stacktrace gets printed three times for some reason). I believe two out of these three stacktraces get logged before the server even gets back to the final method for the REST call.

Either way, as long as I figure out how to suppress one of these logging actions I'll figure out a way to stop all three. Does anyone have an idea how to suppress this kind of logging?

Upvotes: 1

Views: 186

Answers (1)

Izbassar Tolegen
Izbassar Tolegen

Reputation: 2152

As it said in the EJB Specification every SystemException must be logged by the container implementation. You can try to catch it or mark as ApplicationException but if you mark it it won't rollback the transaction. I suggest this:

@Stateless
@TransactionManagement(TransactionManagementType.BEAN)
public class MyBean {

    @Resource
    private UserTransaction tx;

    public void myMethod() throws MyApplicationException {
        try {
            tx.begin();
            // call needed methods
            tx.commit();
        } catch(Exception e) {
            // silently rollback;
            // exceptions from `UserTransaction` omitted for readability
            tx.rollback();
            throw new MyApplicationException(e);
        }
    }
}

Now in your client code of that EJB you can react to MyApplicationException and return whatever you want or log it or don't. By using container managed transactions will ensure that errors are logged by specification (and they are wrapped to another exceptions as bean instances are being destroyed). Also you can mark transaction as rollback-only. Be sure to use this carefully. If you don't want logs from container you need to control all of your flow by yourself.

Upvotes: 1

Related Questions