Olá gente,
Estou com um problema de excesso de conexões no postgresql em uma aplicação web com spring-mvc e hibernate, por isso estou tentando usar um pool de conexões utilizando o c3p0.
A princípio eu tentei fazer o hibernate gerenciar o pool.
Porém, não consegui encontrar o que eu preciso mudar na ConnectionFactory.
Está tudo funcionando okay, mas ele não está gerenciando as conexões e continua explodindo o número de conexões do banco.
Alguém pode me ajudar?
Eu configurei o c3p0 no hibernate.cfg.xml dessa forma:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/fxdb</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">123</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">600</property>
<property name="hibernate.c3p0.timeout">1800</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">1800</property>
<property name="hibernate.c3p0.validate">true</property>
<property name="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property>
<mapping class="com.fxdb.model.DiseaseHistory"/>
<mapping class="com.fxdb.model.File"/>
<mapping class="com.fxdb.model.IntoleranceHistory"/>
<mapping class="com.fxdb.model.MaximumScore"/>
<mapping class="com.fxdb.model.MutationsBase"/>
<mapping class="com.fxdb.model.Lifestyle"/>
<mapping class="com.fxdb.model.Person"/>
<mapping class="com.fxdb.model.PersonMutation"/>
</session-factory>
</hibernate-configuration>
Estou criando as conexões desta forma:
public class ConnectionFactory {
public Session getSession() throws HibernateException {
Configuration configuration = new Configuration();
configuration.configure();
//SessionFactory factory = configuration.buildSessionFactory(null);
//serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
return session;
}
}
Uma DAO apenas para conhecimento:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.fxdb.dao;
import com.fxdb.model.DiseaseHistory;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
/**
*
* @author mariana
*/
public class DiseaseHistoryDAO {
private Transaction tx;
private ConnectionFactory connectionFactory;
private Session session;
public Session getSession() throws HibernateException {
this.connectionFactory = new ConnectionFactory();
this.session = this.connectionFactory.getSession();
return session;
}
public DiseaseHistory save(DiseaseHistory diseaseHistory) {
try {
this.session = getSession();
this.tx = this.session.beginTransaction();
this.session.save(diseaseHistory);
tx.commit();
return diseaseHistory;
} catch (Exception E) {
System.out.println("Exception = " + E.toString());
return null;
}
}
public void update(DiseaseHistory diseaseHistory) {
try {
this.session = getSession();
this.tx = this.session.beginTransaction();
this.session.update(diseaseHistory);
this.tx.commit();
} catch (Exception E) {
System.out.println("Exception = " + E.toString());
}
}
public DiseaseHistory findById(Integer id) {
try {
DiseaseHistory diseaseHistory = new DiseaseHistory();
this.session = getSession();
this.tx = this.session.beginTransaction();
Query query = session.getNamedQuery("DiseaseHistory.findById");
query.setInteger("id", id);
diseaseHistory = (DiseaseHistory) query.uniqueResult();
this.tx.commit();
this.session.close();
return diseaseHistory;
} catch (Exception E) {
System.out.println("Exception = " + E.toString());
return null;
}
}
public List<DiseaseHistory> listAll() {
try {
DiseaseHistory diseaseHistory = new DiseaseHistory();
List lista = new ArrayList<DiseaseHistory>();
this.session = getSession();
this.tx = this.session.beginTransaction();
Query query = session.getNamedQuery("DiseaseHistory.findAll");
lista = query.list();
this.tx.commit();
this.session.close();
return lista;
} catch (Exception E) {
System.out.println("Exception = " + E.toString());
return null;
}
}
public void delete(DiseaseHistory diseaseHistory) {
try {
this.session = getSession();
this.tx = this.session.beginTransaction();
this.session.delete(diseaseHistory);
this.tx.commit();
} catch (Exception E) {
System.out.println("Exception = " + E.toString());
}
}
}