SQL错误:0,SQL状态:25P02(当前事务已中止),没有事务。

3
我正在使用Hibernate和JPA运行以下方法。问题是,当FETCH_MAIL_VERDICTS(一个动态创建的select语句字符串)出错并引发SQLGrammarException时,由于“SQL错误:0,SQLState:25P02”(Postgres)错误,其他无关的查询(从不相关的EntityManager创建)也停止响应。谷歌说这是由于没有回滚错误查询造成的,但这里没有事务。我还尝试在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;
}

“但这里没有交易。” - 每个语句都会启动一个事务,即使是SELECT语句。因此,确实存在一个正在进行的事务。 - user330315
如果是这样,我该如何回滚该事务?我已经在 catch 子句中添加了以下语句,但并未成功:if(em.getTransaction().isActive()) em.getTransaction().rollback(); - randomVariable
不确定您的混淆层如何知道(或不知道)是否正在进行事务。 但是,如果您只是调用 Connection.rollback(),那么应该没问题。 - user330315
在我的情况下,我遇到了数据库控制台/终端会话中的问题。我必须运行 COMMIT,这触发了 ROLLBACK 并允许事务关闭。 - th3coop
1个回答

4

我遇到了相同的错误(SQL Error: 0, SQLState: 25P02)。在查看整个日志后,我发现之前有一个错误触发了这个错误。在我的情况下,问题是因为表上没有授权。由于在同一事务中,所以会抛出异常。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接