Bonjour,
j'ai mis en place une application J2EE avec hibernate 3, postgresql 8.1 et tomcat4.1. Mon application se connecte à trois bases de données (BVM, CRM & CS).
Le problème est que les process postgres se multiplient à chaque appel sans se fermer ce qui aboutit à une saturation de la mémoire (java heap space).
J'utilise le plugin hibernate synchronizer pour générer les dao à partir de mes fichiers de mappings et mes java bean. Les dao je les ai adapté de façons à ce qu'ils utilisent HibernateUtil qui gère les sessions :
Code:
public class HibernateUtil {
private static final SessionFactory bvmSessionFactory;
private static final SessionFactory crmSessionFactory;
private static final SessionFactory csSessionFactory;
static {
try {
String pathFile = "WEB-INF/config/bvm.cfg.xml";
if(!(System.getProperty("bvm.cfg.xml")==null)){
pathFile = System.getProperty("bvm.cfg.xml");
}
File bvmFile = new File(pathFile);
bvmSessionFactory = new Configuration().configure(bvmFile).buildSessionFactory();
pathFile="WEB-INF/config/crm.cfg.xml";
if(!(System.getProperty("crm.cfg.xml")==null)){
pathFile=System.getProperty("crm.cfg.xml");
}
File crmFile = new File(pathFile);
crmSessionFactory = new Configuration().configure(crmFile).buildSessionFactory();
pathFile="WEB-INF/config/cs.cfg.xml";
if(!(System.getProperty("cs.cfg.xml")==null)){
pathFile=System.getProperty("cs.cfg.xml");
}
File csFile = new File(pathFile);
csSessionFactory = new Configuration().configure(csFile).buildSessionFactory();
} catch (HibernateException ex) {
throw new RuntimeException("Exception building SessionFactory: " + ex.getMessage(), ex);
}
}
public static final ThreadLocal bvmSession = new ThreadLocal();
public static final ThreadLocal crmSession = new ThreadLocal();
public static final ThreadLocal csSession = new ThreadLocal();
public static Session getBvmSession() throws HibernateException {
Session s = (Session) bvmSession.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = bvmSessionFactory.openSession();
bvmSession.set(s);
}
return s;
}
public static void closeBvmSession() throws HibernateException {
Session s = (Session) bvmSession.get();
bvmSession.set(null);
if (s != null){
s.close();
}
}
public static Session getCrmSession() throws HibernateException {
Session s = (Session) crmSession.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = crmSessionFactory.openSession();
crmSession.set(s);
}
return s;
}
public static void closeCrmSession() throws HibernateException {
Session s = (Session) crmSession.get();
crmSession.set(null);
if (s != null){
s.close();
s=null;
}
}
public static Session getCsSession() throws HibernateException {
Session s = (Session) csSession.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = csSessionFactory.openSession();
csSession.set(s);
}
return s;
}
public static void closeCsSession() throws HibernateException {
Session s = (Session) csSession.get();
csSession.set(null);
if (s != null){
s.close();
s=null;
}
}
}
Merci pour votre aide, ça fait un moment que je tourne en rond!