I have a method that I use to gather data for a report, while running I monitor the PostgreSQL server and I see that the system does not close the transacition after they end.
The problem is that in time, the server crash out of memory
public Integer ogetNumber() {
EntityManager em = emf.createEntityManager();
Object obj = null;
try {
Query qry = em.createQuery("SELECT MAX(p.number) FROM Number p");
obj = qry.getSingleResult();
if (obj == null) {
return 0;
}
return (Integer) obj;
} catch (Exception e) {
System.out.printf(e.getMessage());
} finally {
if (em != null) {
em.close();
}
}
return (Integer) obj;
}
My persistence.xml:
<?xml version="1.0" encoding="UTF-8"?> <persistence version="1.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_1_0.xsd">
<persistence-unit name="PersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>entities.Validacion</class>
<class>entities.ValidacionDB</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.connection.username" value="postgres"/>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
<property name="hibernate.connection.password" value="password"/>
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/test"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>