Hibernate + PostgreSQL - Connection with state "Idle in transaction"

Viewed 6514

I have a JSF application running on Tomcat using Hibernate. I have made a little test that exposed te problem - A servlet was created, and the only thing it does is this:

EntityManager em=null;
try {
    em = ConnectDb.getEntityManager();
    em.createQuery("from Frete").getResultList();
} catch (Exception e) {
    e.printStackTrace();
} finally {
    try{em.close();}catch(Exception ex){}
}

If i execute the following sql in pg_admin, i get one record, at the column "query" i see the sql executed by hibernate, and at the column "state" it says "idle in transaction".

But the problem is that no transaction was started, the only thing i did was create a query. And the EntityManager was closed - there was no exception thrown when closing it.

Maybe this is a configuration at persistence.xml, but i don't know. Here is my persistence.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="GestaoPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
      <property name="hibernate.show_sql" value="true"/>
      <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
      <property name="hibernate.connection.autocommit" value="false"/>
      <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost/AtualyGestao"/>
      <property name="javax.persistence.jdbc.password" value="atg4747"/>
      <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
      <property name="javax.persistence.jdbc.user" value="AtualyGestao"/>
      <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
      <property name="hibernate.format_sql" value="false"/>
      <property name="hibernate.connection.encoding" value="ISO-8859-1"/>
    </properties>
  </persistence-unit>
</persistence>
3 Answers

I was also hitting this issue in multiple places. Later replaced Hibernate with JDBC and never hit this issue again!

Related