-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 
Author Message
 Post subject: Best Way to deal with SessionFactory in Web App
PostPosted: Mon Nov 15, 2004 10:40 am 
Newbie

Joined: Sun Nov 14, 2004 1:28 am
Posts: 2
We all know that open a sessionFactory and reading a property file or hibernate.cfg.xml takes time. This should not be done for each query/update request coming from the web client. But what is the best way to do it? At webserver startup time and then cache it? Any suggestions would be highly appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 15, 2004 10:41 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
First chapter, reference documentation.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject: Best Way to deal with SessionFactory in Web App
PostPosted: Mon Nov 15, 2004 3:34 pm 
Beginner
Beginner

Joined: Fri Mar 12, 2004 8:40 pm
Posts: 30
Location: SF Bay Area
You can do the configuring once in a class like this one.
When you need a session call

session = SessionHelper.openSession();

package com.rhi.bob.common.dao.impl.hibernate;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;

public class SessionHelper {

private static SessionFactory sessionFactory;
protected static Log log = LogFactory.getLog(SessionHelper.class);

static {
try {
Configuration conf = new Configuration();
conf.configure(); // to load default "hibernate.cfg.xml"
sessionFactory = conf.buildSessionFactory();
} catch (HibernateException he) {
String[] msgs = he.getMessages();
for ( int i = 0 ; i < msgs.length ; i++ ) {
log.error(msgs[i]);
}
he.printStackTrace();
}
}

public synchronized static Session openSession() throws HibernateException {
return sessionFactory.openSession();
}

public synchronized static void close(Session session) {
if(session == null) {
return;
}

try {
session.close(); // disconnect session
} catch(Exception e) {
e.printStackTrace();
log.error("failed to commit a session: " + e);
}
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 15, 2004 3:35 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
This class is broken (it swallows exceptions). Please, use the one from the Hibernate reference documentation, not one from a very bad book.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 15, 2004 3:45 pm 
Senior
Senior

Joined: Wed Mar 24, 2004 11:40 am
Posts: 146
Location: Indianapolis, IN, USA
you could look into Spring. it has built in DAO Support for Hibernate as well as JDBC based DAOs and does a great job.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 15, 2004 4:20 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
if you go with spring, be sure to do it not only for a sessionFactory singleton!

download caveatemptor and just use hibernatefilter and hibernateutil classes, easy ;)

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 15, 2004 5:50 pm 
Senior
Senior

Joined: Wed Mar 24, 2004 11:40 am
Posts: 146
Location: Indianapolis, IN, USA
i just downloaded the source code for caveatemptor to look at the use of HibernateUtil. i noticed that you are using HibernateFilter to commit transactions and close the HibernateSession, which is a nifty trick to manage the session per request type of DAO architecture.

however, i do have a conceptual question and maybe someone can help me out with this. i usually build my applications using filters as the layer that sits between the user and the request in order to "filter" the request to the appropriate transition state. the caveatemptor style of architecture has the filter as a layer that sits between the application and the response. i do not doubt one bit that it works exactly as advertised. but isn't it an unconventional use of Filters.

i am sure some of this thought process is due to my preconceived notions about the use of filters. i would appreciate it if any of the "forerunners" (sorry, playing too much halo2) could help with a conceptual explanation of this utilization of filters. also, how would the application work if the user terminated the session before the response could be sent back to the browser? would that keep the session open pending GC?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 15, 2004 6:01 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Uhm, try Hibernate in Action :)

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 15, 2004 6:16 pm 
Senior
Senior

Joined: Wed Mar 24, 2004 11:40 am
Posts: 146
Location: Indianapolis, IN, USA
christian wrote:
Uhm, try Hibernate in Action :)


i probably should get a copy anyway. :)

but i was hoping for a brief synopsis on this particular question. it is just that i get a bit nervous keeping database sesssion related stuff so close to the end points of the application i.e. next to the request/response.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.