Jbo 4.2/Hibernate 3
Pretty much following the Transaction demo in Trailblazer demo:
http://trailblazer.demo.jboss.com/EJB3Trail/services/transaction/index.html
This sample shows rolling back an update transaction which works fine for me. The problem is that if my session bean is adding a new record it does not roll back when I throw a TransException.
In the code sample below "rec" is updated and "rec2" is a new record. I try to force a rollback by throwing a TransException. The rec update is rolled back but the new row (from rec2) is still persisted:
OutboundQueue rec = em.find(OutboundQueue.class, Integer.valueOf(1503)); rec.setMessage("This message has been updated:" + new Date()); OutboundQueue rec2 = new OutboundQueue(); rec2.setGroupId("1"); rec2.setMessage("123123123"); em.persist(rec); em.persist(rec2); if (rec != null) throw new TransException();
@PersistenceContext(type = PersistenceContextType.TRANSACTION) protected EntityManager em;
@TransactionAttribute(TransactionAttributeType.REQUIRED)
em.setFlushMode(FlushModeType.COMMIT);
Turns out that the problem was due to the fact that my Table was MyISAM. Changed it to InnoDB and it works fine now. What confuses me is why update "rollbacks" seemed to work properly.