I have a J2EE application with tomcat, hibernate 3 and Postgresql.
This application needs to get data from several databases (BVM,CS,CRM). As it is explained in the hibernate guide, I have a HibernateUtil managing the 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;
}
}
}
Problem is that I have more and more postgresql processes which are not finished until tomcat is shutdown (all process killed by the end of the jvm) so I have a java heap space error.
If someone could give me a piece of advice it would be great.[/code]