java - Catch EJBTransactionRolledbackException before it is logged to file -
my setup:
i have ejb bean (deployed in ear on jboss 6.4) method calculates values , stores in database. method started in new transaction. looks this:
@transactionattribute(transactionattributetype.requires_new) public void dostuff() { myobject value = calculatesomevalues(); entitymanager.merge(value); entitiymanager.flush(); }
by design, possible flush fail due constraintviolationexception
. new transaction, exception wrapped in ejbtransactionrolledbackexception
, thrown runtimeexception
. have set logging ejb exceptions useful find them in whole system. know constraintviolationexception occur , then, catch , rollback before logging system logs error (the constraint violation not seen exception, required database's point of view). @ moment, log file clogged entries one:
error [org.jboss.as.ejb3.invocation] (default-threads - 49) jbas014134: ejb invocation failed on component
how can catch exception in such way logger prevented printing these errors? (note: still want log other ejbtransactionrolledbackexception
s)
i don't have jboss 6 deployed, cannot test whether precise exception types work jboss 6 (i tried on wildfly 8).
a possible solution put in ejb method annotated @aroundinvoke (at least if don't have other interceptors on ejb):
@aroundinvoke public object myconstraintviolationinterceptor(invocationcontext ctx) throws exception { try { return ctx.proceed(); } catch(persistenceexception e) { if (e.getcause() instanceof org.hibernate.exception.constraintviolationexception) { //some action taken } else { throw e; } return null; } }
by way, if constraintviolationexception optimistic locking exception in disguise (that is, thrown because user has modified data in meantime), maybe better report user.
Comments
Post a Comment