-->
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.  [ 2 posts ] 
Author Message
 Post subject: Managing long-lived Sessions optimistically
PostPosted: Thu May 13, 2004 4:54 pm 
Regular
Regular

Joined: Fri Dec 12, 2003 2:09 pm
Posts: 84
Location: San Francisco, USA
I wanted to confirm with the community a conclusion I've come to about long-lived Sessions, following the Session-per-App-Transaction pattern. In a nutshell, this is that applications are expected to manage Sessions "optimisitically" -- do not repeatedly check if persistent objects in the Session are stale (inconsistent with the database state) but rather assume they're up to date. This is analogous to the optimistic locking approach Oracle recommends for working with the database itself: assume data has not been modified by another transaction.

I understand why, in general, the optimistic approach scales best. However, the web app I'm developing is in some ways closer to a thick client/Swing app as we expect relatively few concurrent users with large "working sets" in each user's Session. That's why we favor the Session-per-App-Txn pattern.

But we keep bumping into the recommendation in the docs that Sessions should be discarded whenever an exception is thrown; e.g., by a version check failure encountered while locking with LockMode.READ. I posted an inquiry about locking in the beginner's forum.

Then there are notes in the Javadoc, like this one for Session.refresh():

Quote:
Re-read the state of the given instance from the underlying database, with the given LockMode. It is inadvisable to use this to implement long-running sessions that span many business tasks. This method is, however, useful in certain special circumstances.

What kind of "special circumstances" are envisioned? What are good rules of thumb for apps using long-lived Session to do version checks and/or refresh persistent objects? Is this discouraged for performance reasons, to keep things simple, or what?

The user manual suggests that it's OK to force version checks when reconnection long-lived Sessions:

Quote:
Session.reconnect() obtains a new connection (or you may supply one) and restarts the session. After reconnection, to force a version check on data you aren't updating, you may call Session.lock() on any objects that might have been updated by another transaction.

Is the expectation that the Session will be discared, as opposed to just refreshing the stale data? That seems to defeat the purpose of long-lived Sessions.

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 14, 2004 1:13 pm 
Regular
Regular

Joined: Fri Dec 12, 2003 2:09 pm
Posts: 84
Location: San Francisco, USA
Upon further investigation I've learned that the locking mechanism is not a reliable way to check an object version number in that locks can only be acquired once per transaction. Hence, only the first time a lock is acquired within a transaction will Hibernate actually access the database; successive attempts do nothing.

This makes sense from the perspective of how locks are managed, since Hibernate just delegates this responsibility to the database and database locks can usually only be revoked by rolling back the transaction or released by committing the transaction. I can only see the logic for this behavior in terms of an optimistic locking strategy: why should an application repeatedly check an object's version number within a transaction? With long-lived Sessions, checking the version number each time the Session is reconnected (starting a new transaction) should be sufficient.

Hope this information is useful to some of you; perhaps it was self evident to most ;)


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