我在我的web應用程序中使用EclipseLink,並且我很難正常捕捉和處理它生成的異常。我從this thread看到似乎是類似的問題,但我沒有看到如何解決或修復它。如何從EclipseLink捕獲違反約束的異常?
我的代碼如下所示:
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
}
}
當堅持()被調用,違反唯一性約束的實體,我得到的是捕獲並記錄容器例外爆炸。
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提供者一起工作(甚至不能編譯)。 – AlanObject