请求处理失败;嵌套异常是org.springframework.transaction.IllegalTransactionStateException

时间:2014-03-03 13:34:56

标签: java spring spring-mvc spring-transactions

你能否帮我解决这个错误

  

HTTP状态500 - 请求处理失败;嵌套异常是   org.springframework.transaction.IllegalTransactionStateException:   事务已经完成 - 不要再调用commit或rollback   每次交易不止一次

代码

@Override
    public String saveTemplatesToPCA(List<FilesTemplateDomain> fileTemplates) {
         TransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
         TransactionStatus status = transactionManager.getTransaction(transactionDefinition);
        try {
            template.saveOrUpdateAll(fileTemplates);
            transactionManager.commit(status);

            return "success";
        } catch (Exception e) {
            transactionManager.rollback(status);
            System.out.println("Exception in saveTemplatesToPCA() : "+e);
            return "fail";
        }
    }

1 个答案:

答案 0 :(得分:3)

处理此例外

@Override
    public String saveTemplatesToPCA(List<FilesTemplateDomain> fileTemplates) {
         TransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
         TransactionStatus status = transactionManager.getTransaction(transactionDefinition);
        try {
            template.saveOrUpdateAll(fileTemplates);
            transactionManager.commit(status);

            return "success";
        } catch (Exception e) {
            try{
            transactionManager.rollback(status);
            }catch(Exception ee){
                System.out.println("Exception in commit or rollback : "+ee);
            }
            System.out.println("Exception in saveTemplatesToPCA() : "+e);
            return "fail";
        }
    }