Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>
like image 916
Mateus Viccari Avatar asked Dec 24 '13 12:12

Mateus Viccari


People also ask

What causes idle in transaction Postgres?

idle in transaction: This indicates the backend is in a transaction, but it is currently not doing anything and could be waiting for an input from the end user.

How do I disable idle connection in PostgreSQL?

Kill an Idle Connection: We have to provide the process 'id' within the query in a terminate function. >> SELECT pg_terminate_backend(7408); The process has been magnificently killed.

Can Hibernate be used with Postgres?

PostgreSQL is one of the most popular relational databases, and Hibernate is probably the most popular JPA implementation. So, it's no surprise that they're often used together and that you don't have to expect any major issues doing that.

How do I find idle transactions?

If you want to see how many idle connections you have that have an open transaction, you could use: select * from pg_stat_activity where (state = 'idle in transaction') and xact_start is not null; This will provide a list of open connections that are in the idle state, that also have an open transaction.


1 Answers

the only thing i did was create a query

It seems you also execute it :

em.createQuery("from Frete").getResultList();

When you execute this line: hibernate execute the query and to do so a transaction is created by postgresql driver.

Since the auto-commit is set to false :

  <property name="hibernate.connection.autocommit" value="false"/>

then transaction is not commited, and that's why you get this message.

BTW : instead of

try{em.close();}catch(Exception ex){}

write this :

try{em.close();}catch(Exception ex){ex.printStacktrace();}
like image 159
ben75 Avatar answered Oct 14 '22 02:10

ben75