這是我的代碼的一部分。我正在使用JTA事務,這段代碼拋出了唯一的約束異常。如何做容器管理的JTA事務中的異常處理
@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);
}
我寫了這個代碼來處理異常:
public int createProfile(AccountBean accountInfo){
try{
return createProfileHelper(accountInfo);
}catch(Exception e){
logger.error(e);
logger.error(e.getMessage());
return 0;
}
}
令我驚訝,我不能趕上我try catch塊例外,雖然我可以看到除了在服務器上閃爍:
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
我認爲異常仍然被捕獲,儘管事務正在回滾。你確定不是這樣嗎? – Magnilex 2015-03-13 13:44:06
是的:(我在catch裏面放了一個斷點,我無法打到它 – JackSparrow 2015-03-13 13:49:48