Search code examples
transactionsjpa-2.0java-ee-6ejb-3.1

Debugging transactions in Java EE


I have an @Stateless EJB method in which I

  1. delete some entries from a database using JPA remove()'s
  2. throw an Exception that is annotated as @ApplicationException(rollback=true)

I have no other transaction-specific annotations for the method (I set @TransactionAttribute(TransactionAttributeType.REQUIRED) but that should be the default anyway!). Transactions are container managed. JPA provider is EclipseLink.

And still, the transaction is not rolled back when the exception is thrown. Eg. the entries that I deleted from the database before the rollback don't come back. Btw. I call entityManager.flush() before the throw, can it cause such behaviour (it shouldn't)?

I also tried to call SessionContext.setRollbackOnly(), with the same result.

How can I debug this problem?

I'm using Glassfish v3 and Netbeans for debugging, but I'd be equally happy with println's I just don't know where to put them...


Solution

  • I have an @Stateless EJB method in which I (...)

    Just to clarify how do you get the EntityManager?

    (...) I set @TransactionAttribute(TransactionAttributeType.REQUIRED

    Indeed, this is the default and shouldn't be required anyway.

    And still, the transaction is not rolled back when the exception is thrown. Eg. the entries that I deleted from the database before the rollback don't come back.

    Hmm, that's very strange and unexpected.

    Btw. I call entityManager.flush() before the throw, can it cause such behaviour (it shouldn't)?

    No, flush != commit

    I also tried to call SessionContext.setRollbackOnly(), with the same result.

    Well, still unexpected (but at least consistent...).

    I'm using Glassfish v3 and Netbeans for debugging

    Maybe activate the logging of the following categories (e.g. via the admin Console under Configuration > Logging > Log Levels) to see if you can spot anything weird:

    • javax.enterprise.system.core.transaction
    • javax.enterprise.resource.jta
    • javax.enterprise.system.container.ejb

    As an alternative (sort of "poor man's logging"), you could implement SessionSynchronization to get notified about transaction.

    Really weird problem...

    See also