2
My system presented sporadic crashes and I ended up discovering that this happened due to processes that opened a connection but, due to some error, did not close it (Connection Leak).
I solved involving the problematic code in a try/catch
and forcing the closure on finally
My question is: Is there any way to prevent this leak or some timeout setting where, after some downtime, the connection is returned/closed?
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<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="udPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>br.com.xpto.etc</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.format_sql" value="false"/>
<property name="hibernate.multiTenancy" value="SCHEMA"/>
<property name="hibernate.tenant_identifier_resolver" value="br.com.multitenant.SchemaResolver"/>
<property name="hibernate.multi_tenant_connection_provider" value="br.com.multitenant.MultiTenantProvider"/>
<property name="hibernate.enable_lazy_load_no_trans" value="true"/>
<property name="hibernate.c3p0.acquire_increment" value="2"/>
<property name="hibernate.c3p0.iddle_teste_period" value="70"/>
<property name="hibernate.c3p0.max_size" value="15"/>
<property name="hibernate.c3p0.max_statements" value="0"/>
<property name="hibernate.c3p0.min_size" value="1"/>
<property name="hibernate.c3p0.timeout" value="60"/>
</properties>
</persistence-unit>
</persistence>
Entitymanagerfactory
public class FabricaEntityManager {
private static FabricaEntityManager INSTANCE;
private static EntityManagerFactory emf;
private final static Logger log=Logger.getLogger(FabricaEntityManager.class);
private FabricaEntityManager() {}
public static FabricaEntityManager getInstance() {
if (INSTANCE == null) {
synchronized (FabricaEntityManager.class) {
if (INSTANCE == null) {
INSTANCE = new FabricaEntityManager();
log.debug("Nova Fabrica");
}
}
}
return INSTANCE;
}
public EntityManagerFactory getEntityManagerFactory(){
if (emf==null){
emf=Persistence.createEntityManagerFactory("udonlinePU");
log.debug("Create EMF");
}
return emf;
}
}
Trade example
public EntityManager getEntityManager(){
if (manager == null || !manager.isOpen()) {
manager=FabricaEntityManager.getInstance().getEntityManagerFactory().createEntityManager();
log.debug(">> CreateEntityManager");
}
return manager;
}
public List<Despesa> getByAll(){
Query qry=getEntityManager().createQuery("select d from Despesa d ");
List<Despesa> rs = qry.getResultList();
getEntityManager().close();
return rs;
}
Good afternoon you could work with JPA + Hibernate and let the contender manage your connections.
– paulohddias
You mean to create the pool in Tomcat and let it manage the connections? If so, see that I’m working with Multitenant / multi schema. How would I do that to Tomcat’s pool?
– NilsonUehara
Opening and closing connections in each operation decreases performance. Use an API to manage your pool.
– Intruso