I have a situation witch I need my transaction to keep active after an handable persistence error occurs. But HibernateEntityManager sets my transaction as STATUS_MARKED_ROLLBACK before i can threat it. Here is my flow:
Code:
@PersistenceContext(unitName="MY_PU")
EntityManager entityManager;
@Inject
UserTransaction ut;
public void action() throws NotSupportedException, SystemException{
ut.begin();
Person p = new Person();
p.setIdPerson(1L);
//Will force a errors because this field has only 5 characters on database
p.setName("abcdef");
try{
entityManager.persist(p);
entityManager.flush();
}catch (Exception e) {
System.out.println("Here a persistence exception, but i need to continue my transaction");
e.printStackTrace();
}
p.setName("abc");
try{
entityManager.persist(p);
//This flush will throw a exception accusing no transaction active!
entityManager.flush();
ut.commit();
}catch (Exception e) {
e.printStackTrace();
System.out.println("Another error, because my transaction is no more active");
System.out.println("Status:" + ut.getStatus());
}
}
This occurs because at org.hibernate.ejb.AbstractEntityManagerImpl there is a error treatment that sets my transaction as Rollback Only when any exception throwned.
Is there some way I can intercept this? Making hibernate do not mark my transaction to rollback?
I am running my app as an WAR with CDI on jboss-as-7.1.1.Final (default standalone configuration).