users@glassfish.java.net

Transaction Rollback not working as expected

From: <glassfish_at_javadesktop.org>
Date: Thu, 12 Apr 2007 04:48:38 PDT

In my application, it looks like EntityManager.persist() operations are not rolled-back with the transaction. Perhaps I am doing something wrong? I am using EJB3 annotated SLSBs. Here is a code fragment:

@Stateless
public class LicenseManagerBean implements LicenseService, LicenseManager {
        @EJB SequenceService seqService;
        @EJB SystemController controller;
        
        @PersistenceContext(unitName=PersistenceUnit.APPLICATION)
        EntityManager em;
        
        @Resource SessionContext context;

        public Long addLicense(LicenseComplex licenseComplex) throws FinancialException {
                ...
                license.setLicenseNo(seqService.getNextNumber(sequence));
                ...
                em.persist(license);
                ...
                try {
                  ...
                } catch (FinancialException e) {
                        context.setRollbackOnly();
                        throw e;
                }
        }

In this code fragment, the call "seqService.getNextNumber(sequence)" increments a counter in another EJB. When the FinancialException is thrown, and the session context is set to rollback only, I can confirm that the counter is rolled-back to its state prior to the method call, but there is a record created by "em.persist(license)". It would seem that calls to EntityManager.merge() are being rolled-back, but any calls to EntityManager.persist() are not. I would expect from my prior EJB experience that rollback would undo everything in the transaction, new and updated entities.

The "FinancialException" is in a package that is unaware of EJB, therefore cannot have an @ApplicationException annotation. That is why I catch the exception, set rollback, and re-throw.

I am using Glassfish v2-b39.

Thanks in advance. This could be a problem for my users if I cannot figure this out.
[Message sent by forum member 'bhar99328' (bhar99328)]

http://forums.java.net/jive/thread.jspa?messageID=212063