0

我们正在使用 Apache 的 openJPA。我正在使用 Java 反射在托管 bean 中调用删除方法。当我尝试删除因违反约束而无法删除的管理对象时,会出现此问题。当我捕获 InvocationTargetException 并检索异常的原因时,它指出不存在要回滚的全局事务。

Caused by: java.lang.IllegalStateException: No Global Transaction exists to rollback.
    at com.ibm.ws.tx.jta.UserTransactionImpl.rollback(UserTransactionImpl.java:349)

如果我进一步查看堆栈跟踪,我可以看到由于违反约束而引发了 SQL 异常。

---- Begin backtrace for Nested Throwables
java.sql.SQLException: [SQL0532] Delete prevented by referential constraint CONSTRAINTNAME in LIBRARY.

有没有办法让我找到 SQL 异常,以便我可以显示一条友好的消息,指出无法执行删除,因为它正在另一个表中使用。

编辑 - 这是代码

public void deleteRowAction(Object list, DataTableTemplate template){
        System.out.println("Delete Row");

        try{

            Object bean = getManagedBean(template.getDataManagerName());

            Method methodDelete = getManagedBean(template.getDataManagerName()).getClass().getMethod(template.getDeleteMethod(), 
                    Class.forName(template.getTableList_rowItemClassName()));

            //Map<String, String[]> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterValuesMap();

            methodDelete.invoke(bean, list);
        }
        catch(PersistenceException pe){
            System.out.println("Persistence Exception Caught");
        }
        catch(NoSuchMethodException nsme){
            nsme.printStackTrace();
        }
        catch(InvocationTargetException ite){

            logException(ite);
            FacesMessage message = new FacesMessage(ite.getCause().getMessage());
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            FacesContext context = this.getFacesContext();
            context.addMessage(null, message);
        }
        catch(IllegalAccessException iae){
            iae.printStackTrace();
        }
        catch(ClassNotFoundException cnfe){
            cnfe.printStackTrace();
        }
    }
4

2 回答 2

0

将您的异常传递给下面的方法,SQLException递归检索。

    try{
        ....
    } catch(PersistenceException pe){
        SQLException sqle = translate(pe);
    } catch(NoSuchMethodException nsme){
        SQLException sqle = translate(nsme);
    }
    ...

public SQLException translate(RuntimeException e) {
    SQLException result = null;
    Throwable throwable = e;
    while (throwable != null && !(throwable instanceof SQLException)) {
        throwable = throwable.getCause();
    }
    if (throwable instanceof SQLException) {
        System.out.println("Found SQLException...")
        result = (SQLException) throwable;
    }
    return result;
} 
于 2013-06-24T15:36:47.750 回答
0

我们正在使用 IBM 的 Websphere。以下代码由 Websphere 自动生成。由于数据约束没有发生删除,因此回滚失败。这会导致回滚抛出异常,从而导致原始异常丢失。如果我想查看约束异常(我们不能这样做),我将不得不修改此方法。我们将改为记录通用错误消息。

 @Action(Action.ACTION_TYPE.DELETE)
 public String deleteVerificationDocumentCodeTable(
                VerificationDocumentCodeTable verificationDocumentCodeTable)
                throws Exception {
    EntityManager em = getEntityManager();
                try {
                    utx.begin();
                    em.joinTransaction();
                    verificationDocumentCodeTable = em
                            .merge(verificationDocumentCodeTable);
                    em.remove(verificationDocumentCodeTable);
                    utx.commit();
                } catch (Exception ex) {
                    try {
                        utx.rollback();
                    } catch (Exception e) {
                        ex.printStackTrace();
                        throw e;
                    }
                    throw ex;
                } finally {
                    em.close();
                }
                return "";
}
于 2013-06-24T18:30:59.597 回答