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>
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.
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.
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.
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.
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();}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With