SQL错误:0,SQLState:25P02(当前事务被中止),没有事务

时间:2015-07-22 07:01:39

标签: java hibernate postgresql jpa

我正在使用JPA和Hibernate运行以下方法。问题是,当FETCH_MAIL_VERDICTS(动态创建的select语句字符串)错误并提供SQLGrammarException时,由于“SQL Error:0,SQLState:25P02”(Postgres)错误,其他不相关的查询(从不相关的EntityManagers创建)也会停止响应。 Google表示这是因为没有回滚错误的查询,但这里没有交易。我也尝试在catch语句中回滚以保证安全,但问题仍然存在。

谢谢

public static List<VeMailRecord> getMailVerdicts(HttpServletRequest request, 
        String query, String limit, boolean includeBodies, boolean includeAttachments) {
    String FETCH_MAIL_VERDICTS = "SELECT * FROM ve_mail_records ";

    if(query != null && query.length() > 0)
        FETCH_MAIL_VERDICTS += "where origin_id = -1 and " + query;
    else
        FETCH_MAIL_VERDICTS += "where origin_id = -1";

    if(!FETCH_MAIL_VERDICTS.contains("order by")) //Default order
        FETCH_MAIL_VERDICTS += " order by receive_time desc ";

    int limitInt, limitConfig;
    try{
        limitInt = Integer.parseInt(limit);
        limitConfig = Integer.parseInt(Config.getProperty("analyst.verdict.limit"));

        if(limitInt > limitConfig)
            limitInt = limitConfig;

        FETCH_MAIL_VERDICTS +=  " LIMIT " + limitInt + ";";
    } catch (NumberFormatException e) {
        FETCH_MAIL_VERDICTS +=  " LIMIT " + Config.getProperty("analyst.verdict.limit") + ";";
    }

    EntityManager em = entityManagerFactory.createEntityManager();

    List<VeMailRecord> mailList = null;

    try{            
        mailList = em.createNativeQuery(FETCH_MAIL_VERDICTS, VeMailRecord.class).getResultList();

        if(mailList == null)
            mailList = new ArrayList<VeMailRecord>();//Dummy array list to discriminate 

        //Get attachments of each e-mail
        //VeMailRecord lazy fetches attachments . Force loading attachments
        if(includeAttachments){
            for(VeMailRecord mail : mailList)
            {
                for(AttachmentContainer attachment : mail.getAttachmentList())
                {
                    attachment.getFilename();
                }
            }
        }

        //Remove Bodies
        if(includeBodies){
            for(VeMailRecord mail : mailList)
            {
                mail.setBody("");
            }
        }

    } catch (Exception e) {
        logger.error("Error while querying in function 'getMailVerdicts'.", e);
        mailList = null;
    } finally {
        em.close();
    }

    return mailList;
}

1 个答案:

答案 0 :(得分:0)

我遇到了相同的错误(SQL错误:0,SQLState:25P02)。看完整个日志后,我发现有一个先前的错误触发了该日志,在我的情况下,该错误是因为表上没有授予权限。在同一笔交易中引发了异常