-->
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.  [ 5 posts ] 
Author Message
 Post subject: Struts+Hibernate: Hibernate session lifecycle question
PostPosted: Thu Jun 28, 2007 12:36 pm 
Beginner
Beginner

Joined: Mon Nov 13, 2006 9:43 am
Posts: 30
Hello Forum,

I would be grateful to hear from experienced people what is the correct approach is this case.

The standard situation that I have is following(for example):

I have two hibernate beans:

Code:
Language (id, description...)

Person (id, name, Set<Language> - 1..N relationship).



I'm going to show Person information edit dialog which contains the list of by user spoken languages.

Struts:

Code:
hibernateSession = openHibernateSession();
person = hibernateSession.find(id);
httpSession.setAttribute("EditedPerson", person);
hibernateSession.close()


JSP:
Code:
input type="text" value="${sessonScope.EditedPerson.name}"
....

forEach language from ${sessonScope.EditedPerson.languages} {
  ${language.description}
}



If I do this I'm getting IllegalStateException ot something like that - because the hibernateSession is closed.
So either I have to use some other instance(not those one that I get using "find"), something like

...
Code:
Person editedPerson = new EditedPerson();
editedPerson.setName(person.getName());
editedPerson.setLanguages(person.getLanguages());
...
httpSession.setAttribute("EditedPerson", editedPerson);


or I can put all the attributes(Set<Language> as well) in ActionForm that I use in Struts action and use it on JSP instead of http session attribute.

I found the example of struts and hibernate integration: http://www.laliluna.de/struts-hibernate ... al-en.html

They have always one session object and they don't close it at all. So I have an abstract Action extended from all the others and doing following:

Code:
hibernateSession = servletContext.getAttribute("HibernateSession");
if (hibernateSession == null) {
  hibernateSession = openHibernateSession();
  servletContext.setAttribute("HibernateSession", hibernateSession);
}
...
abstractExecuteMethod(request, response, form... hibernateSession, servletContext)


or I save it in httpSession and have hibernateSession per httpSession.

But in both cases hibernateSession stays not closed!

What does that mean and what is the correct approach?

I hope I wxplained all not very complicated way.

Many thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 28, 2007 5:00 pm 
Beginner
Beginner

Joined: Tue Jun 26, 2007 2:31 pm
Posts: 21
Try this before closing the Session

session.save(object);


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 29, 2007 12:34 am 
Beginner
Beginner

Joined: Mon Nov 13, 2006 9:43 am
Posts: 30
Hi,

thanks for your answer.

I can do it, of course, but why? I'm performing no changes. What should I save?

The thing is that the application is working properly (now, as it's un develoopment), but what happens when many users are working with it?

Regards,

Giorgi


Top
 Profile  
 
 Post subject: Re: Struts+Hibernate: Hibernate session lifecycle question
PostPosted: Fri Jun 29, 2007 4:52 am 
Beginner
Beginner

Joined: Thu Aug 19, 2004 5:36 am
Posts: 30
Location: Italy
I suggest you to take a serious look at OpenSessionInView pattern:

http://www.hibernate.org/43.html

The reading of this could seem a bit complicated for a newbie (as I am), but I can state (on my experience) that it clearify a lot of dark corners in web application development.

In short terms: open Hibernate Session in Struts Action (or before, with a http filter) and keep it open until the end of view rendering.
A better implementation, as stated in this page, is the use of two connections: one for the action (read/write for application logic) and another for the rendering of the view.

Take a look also at "Long conversation":

http://www.hibernate.org/hib_docs/v3/re ... sics-apptx


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 03, 2007 2:06 am 
Beginner
Beginner

Joined: Mon Nov 13, 2006 9:43 am
Posts: 30
Hi,

thanks for you answer and sorry for late reaction :-)

That's a very interesting article - have never come accross to it.

If I understand correctly the session for View stays opened until the "real" action comes and does commit and close...

The good thing is that probably I don't have to rebuild my solutions as I have two abstract struts actions: with session close(used for updates) and without session close(for views - such as start editing...).

Anyway, thank you for that link.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.