Catch PersistenceException or ConstraintViolationException in JBoss AS 7

I am involved in the transition from JBoss AS 6 to JBoss AS 7 and am experiencing problems with my tests. Let a simple EJB be assumed:

@Entity public class MyTest implements Serializable { @Id @GeneratedValue(strategy=GenerationType.AUTO) private long id; @NotNull private String headline; } //getter/setter 

In my @Stateless Bean I am doing something like this (as before with JBoss5 and JBoss6):

 @Inject private EntityManager em; public <T extends Object> T persist(T o) throws MyContraintViolationException { System.out.println("***************** persist:"); try { em.persist(o); } catch (Exception e) { System.out.println("*************** exception:"); // Further investigation of Exception e, // then throw MyContraintViolationException } } 

This works fine if I do not violate the @NotNull restriction. If headline==null , I get exceptions, but do not enter the catch :

 12:19:45 INFO ******************** persist: 12:19:45 WARN [com.arjuna.ats.arjuna] (management-handler-threads - 2) ARJUNA012125: TwoPhaseCoordinator.beforeCompletion - failed for SynchronizationImple< 0:ffffc0a801fb:4f969a6e:4f058744:9, org.hibernate.engine.transaction.synchronization.internal. RegisteredSynchronization@38eb9b73 >: javax.persistence.PersistenceException: error during managed flush ... Caused by: javax.validation.ConstraintViolationException: Validation failed for classes [my.test.MyTest] during persist time for groups [javax.validation.groups.Default, ] List of constraint violations:[ ConstraintViolationImpl{interpolatedMessage='kann nicht null sein', propertyPath=headline, rootBeanClass=class my.test.MyTest, messageTemplate='{javax.validation.constraints.NotNull.message}'} 

I am glad to see that the error message is much more detailed than in previous versions of JBoss, but how can I catch javax.validation.ConstraintViolationException and throw my own MyContraintViolationException ? Even the debug message ***** exception not printed.

+4
source share
2 answers

If you read the message and the trace of the exception stack, you will see that this exception is not called by the call, but using a flash:

error during managed cleanup

persist does not issue any queries and does not save anything in the database. It simply asks the object manager to make the temporary entity permanent. During flushing (i.e., just before committing a transaction or before Hibernate executes a query that might require this object to be in the database to return the correct results or when flush() explicitly called), then the constraints are checked and executed insert request.

You can explicitly call flush, but this will affect application performance by preventing Hibernate from executing multiple requests and only executing them when necessary. I would just use my own exception. Why do you need such a transformation?

+11
source

You use EJB to contain your entityManager. Each method call to an stateless Ecl file ends with a transaction.

You have entered EntityManager. This means that the EntityManager will also be part of your transaction. The Manager entity is cleared only during the transaction, so you will not see this error message.

What you need to do is the following:

 @Inject private EntityManager em; public <T extends Object> T persist(T o) throws MyContraintViolationException { System.out.println("***************** persist:"); em.persist(o); try { em.flush(); } catch (Exception e) { System.out.println("*************** exception:"); // Further investigation of Exception e, // then throw MyContraintViolationException } } 
0
source

Source: https://habr.com/ru/post/1389445/


All Articles