JackSparrow
JackSparrow

Reputation: 197

how to do exception handling in container managed JTA transaction

This is one part of my code. I am using JTA transaction and this piece of code is throwing unique constraint exception.

@TransactionAttribute( REQUIRED )
            private int createProfileHelper(AccountBean  accountInfo) throws Exception{    
        Long portfolio_customer_id = portfolioCustomerEntity.getId();
                            ExternalAccountEntity externalAccountEntity = new ExternalAccountEntity();
                            externalAccountEntity.setAccountNumber(accountInfo.getAccountNumber().toUpperCase());
                            externalAccountEntity.setBrand(brand);
                            externalAccountEntity.setAccountName(accountInfo.getAccountName());
                            externalAccountEntity.setRepId(accountInfo.getRepId());
                            externalAccountEntity.setCreatedBy(userName);
                            externalAccountEntity.setCreatedDate(new Date());
                            externalAccountEntity.setUserId(userId);
                            externalAccountEntity.setCustomerId(portfolio_customer_id); //join created between external account and portfolio_customer
                            persistenceToolsEntityManager.persist(externalAccountEntity);
    }

I wrote this code to handle exception:

public int createProfile(AccountBean  accountInfo){
        try{
            return createProfileHelper(accountInfo);
        }catch(Exception e){
            logger.error(e);
            logger.error(e.getMessage());
            return 0;
        }
    }

To my amazement I am unable to catch exception in my try catch block though I can see the exception flashing on server :

Mar 13, 2015 9:19:58 AM org.apache.geronimo.transaction.manager.TransactionImpl beforeCompletion
WARNING: Unexpected exception from beforeCompletion; transaction will roll back
javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: ORA-00001: unique constraint (SEC.PORTFOLIO_EXTERNAL_ACCOUNT_U1) violated

Upvotes: 0

Views: 1013

Answers (2)

Howard Wang
Howard Wang

Reputation: 601

Try to put a breakpoint on persistenceToolsEntityManager.persist and step into the code line by line.
You'll find that org.apache.geronimo.transaction.manager.TransactionImpl handles the exception for you.
If you want to do something else when any exception occurs, try to use the TransactionManager and getStatus().
:)

Upvotes: 0

Steve C
Steve C

Reputation: 19445

It's not that amazing really.

Your @TransactionAttribute( REQUIRED ) actually has no effect whatsoever. This annotation can only be applied to public methods, and only has an effect when called from a different bean. In other words, you cannot use annotations to demarcate transactions between method calls within the same bean.

The javax.persistence.PersistenceException has been trapped by the transaction manager upon exiting the createProfile method (which has an implicit @TransactionAttribute( REQUIRED ) by default).

Upvotes: 0

Related Questions