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!