-->
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: Long Sessions (application transactions with long sessions)
PostPosted: Mon Jun 27, 2005 5:02 pm 
Newbie

Joined: Mon Jun 20, 2005 2:51 pm
Posts: 13
Location: Brasil
I need some advice on how to keep objects in the session when working with long sessions (as described in Hibernate in Action p. 325 8.2)

I would like to keep some object always bound to the current session, like for example, the user context object, after logging in to the system. This is very convenient, as the user object is always used somehow in the views.

So I always want to keep the user object around. This works smoothly until I want to start a application transaction, for example when I want to create a new user.

When I need to update an object or create new objects, I start a new application transaction, as shown in the book, and the session is cleared.

request 1: HibernateUtil.newApplicationTx();
..
request n: HibernateUtil.commitApplicationTx();

After that requests, accessing the user object sometimes works, sometimes not. Currently, the only safe way I found is to reload the object in each request, like this:

Code:
      UserDAO userDAO = new UserDAO();
      User user = userContext.getUser();     // get from http session
      user = userDAO.getUserById(user.getId(), false);

I also tried this:

Code:
      UserDAO userDAO = new UserDAO();
      User user = userContext.getUser();
      userDAO.lock(user);


This seems to work, but sometimes I get a lazy-Exception.
So my question: What is the correct way? Always reload the objects in every request?

Thanks in advance,

Axel

_________________
Skype: 'axelberle'


Top
 Profile  
 
 Post subject: Re: Long Sessions (application transactions with long sessio
PostPosted: Tue Jun 28, 2005 2:05 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Axel Wilhelm Berle wrote:
So my question: What is the correct way? Always reload the objects in every request?

Thanks in advance,

Axel


Correct way is to store "userid" in httpsession and It makes sence to use "global" cache if objects is used by all requests (reload it from database or cache once per transaction/request ).


Top
 Profile  
 
 Post subject: Re: Long Sessions (application transactions with long sessio
PostPosted: Tue Jun 28, 2005 9:44 am 
Newbie

Joined: Mon Jun 20, 2005 2:51 pm
Posts: 13
Location: Brasil
Hi,

so if I understand correctly, I really should reload the object with:

Code:
    load(userId, false)


in every request? That was the only way that always worked. In fact, I put the corresponding object in the second level cache, is that what you are suggesting?


Thanks,
Quote:
Correct way is to store "userid" in httpsession and It makes sence to use "global" cache if objects is used by all requests (reload it from database or cache once per transaction/request ).

_________________
Skype: 'axelberle'


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 28, 2005 9:52 am 
Regular
Regular

Joined: Thu Apr 21, 2005 9:05 am
Posts: 50
Location: Boston, U.S
Hi Axel,

As suggested previously, You may consider the usage of secondar level cache. Hibernate is bundled with ecache.

You may enable this cache by setting up the Hibernate.cfg.xml.

<property name="hibernate.cache.use_second_level_cache">true</property>

You may also need the ecachexxx.config.xml file.

Hope it helps.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 28, 2005 11:50 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Yes, this is simple and "safe" way.


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.