-->
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.  [ 7 posts ] 
Author Message
 Post subject: Hibernate Synchronizer : how to manage sessions?
PostPosted: Fri Nov 19, 2004 9:57 am 
Newbie

Joined: Thu Sep 30, 2004 6:00 am
Posts: 13
--------------------------------------------------------------------------------
Hello,

I'm lost with sessions managing.
I never know when I have to do a _rootDAO.initialize, a session closing and a session opening.

Background :
I'm using Struts.
An action calls two DAO methods.

I can't get this action work.

Sometimes it returns me an error like "session is closed".
Sometimes I have warnings in hibernate's logs that tells sessions are not closed.

Where should I declare "_RootDAO.initailize()" ?
In the struts action or inside the two methods?
In the struts action : my two methods are dependant to the action and that's really not MVC.
In the two methods : _RootDAO.initialize would be called two times. Isn't it a loss of performance? What happens when I called it two times?

Here's a pseudo-code below.
I hope somebody can give me the light because I'm really lost...


ACTION =
1. _RootDAO.initialize()
2. xxxDAO.method1();
3. xxxDAO.method2();


xxxDAO.METHOD1() =
Session session = _RootDAO.createSession();
if (!session.isOpen()) session.getSessionFactory().openSession();
try {
Query query = session.getNamedQuery(QUERY_FIND_BY_CODE);
query.setString("code", code);
List sites=query.list();
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}


xxxDAO.METHOD2() =
Page page=new Page();
page.setName(file.getName());
PageDAO pageDao=new PageDAO();
pageDao.save(page);


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 10:31 am 
Senior
Senior

Joined: Fri May 14, 2004 9:37 am
Posts: 122
Location: Cologne, Germany
Hi,

since you are using the Hibernate Sychronizer plugin you only have to call the initialize() method once. It'll create the Sessionfactory and there will be only one even if you are calling initialize twice.

xxxDAO.METHOD1() =
Session session = _RootDAO.createSession();
if (!session.isOpen()) session.getSessionFactory().openSession();
try {
Query query = session.getNamedQuery(QUERY_FIND_BY_CODE);
query.setString("code", code);
List sites=query.list();
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}
--> instead of using session.getSessionFactory.......
xxxDAO.getNamedQuery() --> there also some with params (using version 2.3.1)

xxxDAO.METHOD2() =
Page page=new Page();
page.setName(file.getName());
PageDAO pageDao=new PageDAO(); --> Change this to BasePageDAO.getInstance();
pageDao.save(page); --> while using this method the DAO created by the sychronizer will open a session and so on

BTW I guess you will have to overwrite some method which can be done within the _RootDAO. e.g.: I'm using a ThreadLocal for getting the Session and Transaction, I also have overwritten the Transaction Handling like described in HIA

_________________
regards

Olaf

vote if it helped


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 10:34 am 
Senior
Senior

Joined: Fri May 14, 2004 9:37 am
Posts: 122
Location: Cologne, Germany
Hi,

since you are using the Hibernate Sychronizer plugin you only have to call the initialize() method once. It'll create the Sessionfactory and there will be only one even if you are calling initialize twice.

xxxDAO.METHOD1() =
Session session = _RootDAO.createSession();
if (!session.isOpen()) session.getSessionFactory().openSession();
try {
Query query = session.getNamedQuery(QUERY_FIND_BY_CODE);
query.setString("code", code);
List sites=query.list();
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}
--> instead of using session.getSessionFactory.......
xxxDAO.getNamedQuery() --> there also some with params (using version 2.3.1)

xxxDAO.METHOD2() =
Page page=new Page();
page.setName(file.getName());
PageDAO pageDao=new PageDAO(); --> Change this to BasePageDAO.getInstance();
pageDao.save(page); --> while using this method the DAO created by the sychronizer will open a session and so on

BTW I guess you will have to overwrite some method which can be done within the _RootDAO. e.g.: I'm using a ThreadLocal for getting the Session and Transaction, I also have overwritten the Transaction Handling like described in HIA

_________________
regards

Olaf

vote if it helped


Top
 Profile  
 
 Post subject: Hibernate Synchronizer : how to manage sessions?
PostPosted: Fri Nov 19, 2004 1:51 pm 
Newbie

Joined: Thu Sep 30, 2004 6:00 am
Posts: 13
Thank you very much. Your answer helps me a lot.

Before reading your post, I had changed the code of the _RootDAO template.

in createSession(...) I've added :

....
if (null == session|| !session.isOpen() ) {
session = getSessionFactory(configFile).openSession();
...

So that it re-opens automatically the session if it has been closed.

It worked fine.

But from now I use the xxxDAO.getNamedQuery with a Hashtable for the parameters and it's safe and clean.


You said I should initialize just one time (but initializing a second time is not dangerous).
Then.... Do you think I should initalize this just one time for all the entire tomcat application?
For the moment I do it in every Struts Action.

Do you know a way to run _RootDAO.initalize() juste once in a Tomcat project?
It may be the good solution for me...


Thank you again!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 24, 2004 4:23 am 
Senior
Senior

Joined: Fri May 14, 2004 9:37 am
Posts: 122
Location: Cologne, Germany
Hi,

sorry for the delay in the answer. I normally call the initialize only when the App Server (JBoss with Tomcat) has been started. Should be straight forward with Tomcat. The initialize is called if a user login runs into an Exception due to the fact that the SessionFactory hasn't initialized before.

As mentioned before calling initialize twice doesn't hurt so much, but you'll loose the session caches etc.

_________________
regards

Olaf

vote if it helped


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 24, 2004 6:30 am 
Newbie

Joined: Thu Sep 30, 2004 6:00 am
Posts: 13
Ok...

So I'm going to do a test in the very first struts action.
If the session has'nt been initialized yet, I'll ask for an initialization.

I think I have now all the keys to manage db sessions without errors.

But it remains something weird for me.
As I'm working on a web-application, I'm looking for a MVC schema.
In my mind, the model layer should be completely independant in order to improve reusability.

But in our configuration none of my model objects works if I don't run a _RootDAO.initialize() in the Control layer.

Do you know what I mean?

It doesn't prevent me from running my app correctly but I would be interested if you have an idea about a way to keep the integrity of the MVC conception...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 26, 2004 9:14 am 
Senior
Senior

Joined: Fri May 14, 2004 9:37 am
Posts: 122
Location: Cologne, Germany
You are right, but I don't have a concpet for it either. The initialize calls violates the independancy, but on the other hand this is only done in one place and no where else. So from my point of view, this isn't so critical if it's not violated anywhere else but that has to be tracked and the focus must remain there that the concept isn't violated anywhere else.
Due to the fact that we use Design for Today and I'm not using a web tier at the moment I haven't made some thoughts about it yet, but that will be done in Jan/Feb next year. Perhaps at that stage I can tell you more.

_________________
regards

Olaf

vote if it helped


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