【发布时间】:2011-11-14 23:31:10
【问题描述】:
我在我的 Web 应用程序中使用 EclipseLink,我很难优雅地捕捉和处理它生成的异常。我从this thread 看到似乎是类似的问题,但我不知道如何解决或修复它。
我的代码如下所示:
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() 时,我会遇到大量异常,这些异常会被容器捕获并记录下来。
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 是不可移植的,但我需要从某个地方开始。异常永远不会被捕获。有什么建议吗?
【问题讨论】:
-
“DataBaseException 不可移植”是什么意思?谢谢!
-
@WeishiZeng DataBaseException 类是由 org.eclipse 定义的,而不是 JPA 规范。因此,这可能无法与另一个 JPA 提供程序一起工作(甚至编译)。
标签: jpa jpa-2.0 eclipselink