I catch the exception but still see it thrown in the logs

Viewed 719

I have an application in Weblogic using JPA. Sometimes when I merge objects to DB I get a SQLIntegrityConstraintViolationException. I catch that exception and handle it. The issue is I am still seeing the exception in the logs even though it's caught and handled. Some code:

public class MyClass implements MessageListener {
 public void onMessage(Message message) {
    //do stuff
    SomeItem someItem = processItem(request);
    localEJB.mergeAnotherItem(someItem);

}
private SomeItem processItem(ItemType item) throws Exception {            
        int retry = 0;
            boolean success = false;
            while ((!success) && (retry <= 3)) {
                try {

                    Item dbItem =  myEJB.mergeItem(item); //this causes exception
                    item = dbItem;
                    success = true;
                } catch (TransactionRolledbackLocalException ex) {
                    retry++;
                    if (ex.getCause().toString().contains("SQLIntegrityConstraintViolationException")) {
                    logger.log("TransactionRolledbackLocalException SQLIntegrityConstraintViolationException while merging on try # " + retry);
                        Item dbItem2 = myEJB.findItem(itemName);
                        if dbItem2 != null) {
                            item= dbItem2 ;
                        }
                        if (retry > 3) {
                            //log
                        }
                    } 
                }catch(Exception ex){
                        //log cause, etc
                    }
    }   

}

My EJB code that I'm calling the methods from

@Stateless(name = "MyEJB", mappedName = "MyService-MySessionEJB")
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public class MyEJBBean implements MyEJBSessionEJB,
                                                        MySessionEJBLocal {
    @PersistenceContext(unitName="MyModel")
    private EntityManager em;


    public Item mergeItem(Item item) {
        return em.merge(item); //This throws the SQLIntegrityConstraintViolationException, an internal exception wrapped in TransactionRolledbackLocalException. Which I'm catching in the caller
    }


     /** <code>select o from Items o where o.itemName = :itemName</code> */
         public Item findItem(String itemName) {
             try {
                 return (Item)em.createNamedQuery("Item.findByName").setParameter("itemName", itemName).getSingleResult();
             } catch (NoResultException e) {
                 return null;
             }
         }


}

In the logs I see that the mergeItem sometimes fails. Then it retries it. It works after the first retry. But in the logs I see three entries when it fails

A warning:

    UnitOfWork(738260825)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.1.v20111018-r10243): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLIntegrityConstraintViolationException: ORA-00001: unique constraint (ITEM1_UK) violated

An error:

    Exception occurred during commit of transaction Name=[EJB MyEJB.mergeItem(Item)],Xid=BEA1-29FF28139113B72A3D9D(738260452),Status=Rolled back. [Reason=Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.1.v20111018-r10243): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLIntegrityConstraintViolationException: ORA-00001: unique constraint (ITEM1_UK) violated

And then my log entry when I catch the exception

TransactionRolledbackLocalException SQLIntegrityConstraintViolationException while merging on try # 1

I'm clearly catching the exception but at the same time I still see the exception in the logs. I tries catching Exception e but it never catches that...the exception is the transaction one.

If the Exception is being caught why do I still see it in the logs?

1 Answers
Related