users@glassfish.java.net

JPA (TopLink Implementation) transaction rollback behaviour

From: <glassfish_at_javadesktop.org>
Date: Fri, 18 Jan 2008 06:58:32 PST

Hi all,

I decided to try Glassfish with the new JPA Toplink implementation, using the NetBeans IDE and while everything seemed so perfect i ran onto a very strange problem.
I incluse some statements within a transaction like below, which when commits raises an exception as it should be. The problem is that the very same transaction is inactive when it reaches the catch block, so rollback does nothing and half of the statements are persisted to the database while the ones after the failed one not. My datasource is XADatasource (SQLServer 2005) (1.2 JDBC Driver) and i ve added an autocommit property set to false in the jdbc-connection-pool defined in sun-resources.xml
Anyway Toplink claims to set autocommit to false whenever a transaction.begin() is executed and back to true after a commit(successful only?) or rollback.

After the code below is executed, i find A,B,C inserted into the database and D ofc not.
Exactly what I didnt want ofc:) as I was expecting none of the A,B,C,D entities to be persisted if one or more of them failed.

EntityManager em = Persistence.createEntityManagerFactory("BCLimitAlpha").createEntityManager();
EntityTransaction t = em.getTransaction();
boolean result=true;
try {
t.begin();
em.persist(A);
em.persist(B);
em.persist(C);
em.persist(D); (//lets say it fails cause of db constraint)
t.commit();

} catch (Exception e) {
result = false;
---------> here t.getActive() returns false!!!!!!
t.rollback();
FacesContext context = FacesContext.getCurrentInstance();
context.getExternalContext().log(e.toString());
} finally {
em.close();
return result;
}

Thx in advance,
theHunt
[Message sent by forum member 'thehunt' (thehunt)]

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