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.  [ 19 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: proper place for SessionFactory in struts/web application
PostPosted: Thu Nov 18, 2004 3:46 pm 
Beginner
Beginner

Joined: Tue Nov 02, 2004 1:34 pm
Posts: 45
I'm reading through the eBook, and I have a struts application that uses hibernate that I'm developing.

I have things working....so I'm not asking about a bug.

I'm wondering though, where is the proper place for the SessionFactory. Currently I have <Table>Service.java classes for each of my database tables. They have the basic add/update/query methods.

Each method, I create a SessionFactory....do stuff...then close.

But I'm reading the book and it says that SessionFactory, unlike Session, is an expensive operation...and that you generally have only one session factory for the whole application.

How does that fit in the context of a web based application like one using struts? Where do I put the code to build the SessionFactory such that it's not built and destroyed each time...and is available to subsequent web requests.

Lee


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 18, 2004 3:48 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Chapter 8.

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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 18, 2004 5:31 pm 
Beginner
Beginner

Joined: Tue Nov 02, 2004 1:34 pm
Posts: 45
christian wrote:
Chapter 8.


Clear as mud now :)

BTW, I searched for the code in chapter 8 and couldn't find it in the book source.

Anybody actually impliment the Chapter 8 method in a strust application and could you share your code?

Code for your HibernateUtility...example action....servlet filter.

Thanks,

Lee


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 18, 2004 9:28 pm 
Beginner
Beginner

Joined: Fri Mar 12, 2004 8:40 pm
Posts: 30
Location: SF Bay Area
see caveatemptor.hibernate.org for an example web app set up.

though you could ignore that and just use HibernateUtil from the
reference manual.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 4:21 am 
Regular
Regular

Joined: Fri Nov 12, 2004 12:07 am
Posts: 57
Location: Beijing,China
I am used to define a HibernateUtil(it is not all) class, code as below:

class HibernateUtil{
private static SessionFactory sf=null;
private static final ThreadLocal tl=new ThreadLocal();

public static Session currentSession(){
Session session=sf.openSession();
tl.set(session);
return tl.get();
}

public static void closeSession(){
Session session=tl.get();
if(session!=null&&session.isOpen()){
session.close();
session=null;
}
tl.set(null);

}
}

and a filter:

public class HibernateFilter implements Filter{

public HibernateFilter() {
}

public void init(FilterConfig filterConfig) throws ServletException{

}

public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain filterChain) throws ServletException{

}


public void destroy(){
HibernateUtil.closeSession();
}

}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 4:29 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Both broken, please use the classes from CaveatEmptor.

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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 1:22 pm 
Beginner
Beginner

Joined: Tue Nov 02, 2004 1:34 pm
Posts: 45
Ok...it's not that I'm unwilling to read (RTFM)...I've downloaded the Caveat Emptor code (.95) -- and the readme says it's not a working app. No biggie, still lots of useful code.

I'm confused, though, over the best practices for integration with struts. The example code I found on the web has creation of the Configuration and SesstionFactory in each of the DAO methods....obviously not a good thing since this is a resource intensive operation (according to the Hibernate in Action book).

I do see the HibernateUtility class and the Filter class. But I'm still unclear on how this all fits together.

I see the Hibernate plugin which seems to be a good place to start. It would start up that expensive Configuration and SessionFactory at the beginning of the app...and stores the SessionFactory in a session.

All good.

But if I've done that...then do I still need the HibernateFilter implimented? I would think so. Yet I'm not exactly sure how to impliment the HibernateFilter class in a struts app.

If I'm using the plug in....and the filter....does the rest of the hibernate code in caveat emptor (HibernateUlit, DAO) classes work as coded?

I guess I'm wondering if the following are the steps I need to accomplish:

1. Impliment Hibernate pluggin
2. Impliment HibernateFilter as an app filter (still not sure how to do this)
3. Code my DAO (my app calls them <table>Service but same thing) the same way as the caveat emptor code?

Lee


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 1:24 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
It seems you are really stuck at a very early stage. I recommend Hibernate in Action if all the other documentation doesn't make sense for you.

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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 4:01 pm 
Beginner
Beginner

Joined: Tue Nov 02, 2004 1:34 pm
Posts: 45
christian wrote:
It seems you are really stuck at a very early stage. I recommend Hibernate in Action if all the other documentation doesn't make sense for you.


I'm going through the book now...it's what is spawning my questions.

Yes, I'm at an early stage...in java/struts/hibernate development. I'm not a new developer, as I've been developing applications since 1983. But to struts/hibernate -- yes, I'm new.

But I'm not stupid, slow or lazy.

And I've paid for the book, and I'm reading it...the second of hibernate books I've purchased...having already read "Hibernate: A Developer's Handbook".

And yes, I've downloaded and am reading through the CaveatEmptor sample code.

Now, back to my question. As I've said, currently I have hibernate working....CRUD screens in a web app built with the struts framework.

But the code I currently use -- opens and closes a Connection and ServiceFactory in every DAO method.

I'm looking for a better way to integrate with struts. The eBook suggests using only a single SessionFactory and I'm trying to figure out how to accomplish this.

Reading through the book (Chapter 2), I notice that there are a couple ways to achieve this...one of them is the Singleton pattern.

Which triggered something in my memory, so I went to my Struts book, looked up the Singleton design pattern....and then went to the Hibernateutility to see if I could convert it.

Low and behold, the HibernateUtility is already written as a singleton. Since it uses static variables to hold the Configuration/SessionFactory objects...it won't be creating them new each time I call it.

So...am I correct in understanding that if I use the HibernateUtility -- with the HibernateFilter.....I'm all set?

No need for the Hibernate plugin? No need for JNDI (for this purpose)?

Lee


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 4:17 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Chapter 8.

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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 4:42 pm 
Beginner
Beginner

Joined: Tue Nov 02, 2004 1:34 pm
Posts: 45
christian wrote:
Chapter 8.


I read chapter 8.

I still don't know the answer to the question:

Will using the HibernatUtil class with HibernateFilter set as an app filter suffice?

Lee


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 5:00 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Sure.

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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 5:02 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
P.S. Don't use HibernateFilter unless you really understand what it does. That means, don't use ThreadLocal variables without knowing what a ThreadLocal variable is! There is a very simple variation of HibernateUtil in chapter 8 and in the reference documentation. This is completely sufficient until you are ready to make the next step.

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


Top
 Profile  
 
 Post subject: The fog begins to lift
PostPosted: Fri Nov 19, 2004 5:49 pm 
Beginner
Beginner

Joined: Tue Nov 02, 2004 1:34 pm
Posts: 45
Ok...let me see if I have this straight.

1. I could use JNDI or HTTPServlet.Session variables to hold my app wide instance of SessionFactory...but I don't have to....using Static variables achieves the same result: only one SessionFactory...no having to open and close it for each DAO method.

2. There is the simple version of HibernateUtil that uses static variables. This is all I need to call from my DAO methods....except that I'm vulnerable to lazy fetch issues.

3. The threadLocal version of HibernateUtil that comes with CaveatEmptor needs HibernateFilter to be added as an app filter to close/flush HibernateSessions after the view is rendered. This fixes our lazy fetch issues.

4. I may not understand all the ins and outs of ThreadLocal...but if I get HibernateFilter setup in my Struts controller, the code I personally right doesn't need to worry about it?

Do I understand correctly?

Can you point me to information on setting up HibernateFilter with struts?

:)

Lee


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 5:51 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
All correct.

Sorry, I never used Struts.

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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 19 posts ]  Go to page 1, 2  Next

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.