title: SQL Error: 0, SQLState: 25P02 (current transaction is aborted) with no transaction
body: <p>I am using JPA with Hibernate to run the following method. The thing is, when FETCH_MAIL_VERDICTS (a dynamically created select statement string) is wrong and gives an SQLGrammarException, other unrelated queries (created from unrelated EntityManagers) also stops responding because of "SQL Error: 0, SQLState: 25P02" (Postgres) error. Google says this is caused by not rolling back an erroneous query, but there is no transaction here. I also tried rolling back in the catch statement just to be safe, but the problem still occurs. </p>
  
  <p>Thank you</p>
  
  <pre><code>public static List&lt;VeMailRecord&gt; getMailVerdicts(HttpServletRequest request, 
          String query, String limit, boolean includeBodies, boolean includeAttachments) {
      String FETCH_MAIL_VERDICTS = "SELECT * FROM ve_mail_records ";
  
      if(query != null &amp;&amp; query.length() &gt; 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 &gt; 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&lt;VeMailRecord&gt; mailList = null;
  
      try{            
          mailList = em.createNativeQuery(FETCH_MAIL_VERDICTS, VeMailRecord.class).getResultList();
  
          if(mailList == null)
              mailList = new ArrayList&lt;VeMailRecord&gt;();//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;
  }
  </code></pre>