如何从EclipseLink捕获约束违反异常? [英] How do I catch the constraint violation exception from EclipseLink?

查看:213
本文介绍了如何从EclipseLink捕获约束违反异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Web应用程序中使用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.

我的代码如下:

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
    }
}

当使用违反唯一性约束的实体调用 persist()时,我得到了由容器捕获并记录的异常的爆炸式增长.

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)

我尝试了以下操作:

    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);            
    }

我意识到使用DataBaseException并不是可移植的,但是我需要从某个地方开始.异常永远不会被捕获.有什么建议吗?

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.

事实证明,在这种情况下,使用EclipseLink时,违反SQL约束时可以捕获的异常是 RollBackException ,这是 em.commit()的结果强>通话.因此,我修改了我的persist方法,如下所示:

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);
    }
}

因此,调用方将捕获 EntityExistsException 并采取适当的措施.日志中仍然充满了内部异常,但可以稍后将其关闭.

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.

我意识到这有点滥用 EntityExistsException 的意图,该意图通常仅在重新使用实体ID字段时使用,但出于用户应用程序的目的而使用没关系

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.

这篇关于如何从EclipseLink捕获约束违反异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆