java捕获sql约束异常,如何从EclipseLink捕获约束违反异常?

I am using EclipseLink in my web application, and I am having a hard time gracefully catching and handling Exceptions it generates. I see from this thread what seems to be a similar problem, but I don't see how to work around or fix it.

My code looks like this:

public void persist(Category category) {

try {

utx.begin();

em.persist(category);

utx.commit();

} catch (RollbackException ex) {

// Log something

} catch (HeuristicMixedException ex) {

// Log something

} catch (HeuristicRollbackException ex) {

// Log something

} catch (SecurityException ex) {

// Log something

} catch (IllegalStateException ex) {

// Log something

} catch (NotSupportedException ex) {

// Log something

} catch (SystemException ex) {

// Log something

}

}

When persist() is called with an entity that violates a uniqueness constraint, I get an explosion of exceptions that are caught and logged by the container.

Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504):

org.eclipse.persistence.exceptions.DatabaseException

Internal Exception: java.sql.SQLIntegrityConstraintViolationException: The statement

was aborted because it would have caused a duplicate key value in a unique or

primary key constraint or unique index identified by 'SQL110911125638570'

defined on 'CATEGORY'.

Error Code: -1

(etc)

I have tried the following:

try {

cc.persist(newCategory);

} catch (PersistenceException eee) {

// Never gets here

System.out.println("MaintCategory.doNewCateogry(): caught: " + eee);

} catch (DatabaseException dbe) {

// Never gets here neither

System.out.println("MaintCategory.doNewCateogry(): caught: " + dbe);

}

I realize that using DataBaseException is not portable, but I need to start somewhere. The exceptions never get caught. Any suggestions?

解决方案

It looks like I won't get any more activity on this question, so I will post my work-around and leave it at that. A number of web searches haven't found much of anything that is helpful. I would have thought this is a textbook case but none of the tutorials I have found covers it.

As it turns out in this condition with EclipseLink, the Exception you can catch when the SQL constraint is violated is the RollBackException that is the result of the em.commit() call. So I have modified my persist method like this:

public void persist(Category category) throws EntityExistsException {

try {

utx.begin();

em.persist(category);

utx.commit();

} catch (RollbackException ex) {

Logger.getLogger(CategoryControl.class.getName()).log(Level.SEVERE, null, ex);

throw new EntityExistsException(ex);

} catch (HeuristicMixedException ex) {

Logger.getLogger(CategoryControl.class.getName()).log(Level.SEVERE, null, ex);

} catch (HeuristicRollbackException ex) {

Logger.getLogger(CategoryControl.class.getName()).log(Level.SEVERE, null, ex);

} catch (SecurityException ex) {

Logger.getLogger(CategoryControl.class.getName()).log(Level.SEVERE, null, ex);

} catch (IllegalStateException ex) {

Logger.getLogger(CategoryControl.class.getName()).log(Level.SEVERE, null, ex);

} catch (NotSupportedException ex) {

Logger.getLogger(CategoryControl.class.getName()).log(Level.SEVERE, null, ex);

} catch (SystemException ex) {

Logger.getLogger(CategoryControl.class.getName()).log(Level.SEVERE, null, ex);

}

}

So the caller catches the EntityExistsException and takes the appropriate action. The log still fills up with the internal exceptions but that can be shut off later.

I realize that this is a bit of an abuse of the intent of the EntityExistsException that is normally only used when an entity ID field is re-used, but for the purposes of the user application it doesn't matter.

If anyone has a better approach please post a new answer or comment.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值