0

session initialization

asked 2012-09-20 12:39:43 +0800

avidD gravatar image avidD
166 2

Hello,
I am currently tuning the performance of our application. I want to fetch some objects from the database before needing them. Having fetched these objects, they are put into the user's session. However, the ZK session does not yet exist. At what time is the session created? I would think this should be done as soon as request starts a new session, i.e., before anything is drawn.
So, my problem is, the application is already in my code but the result of Sessions.getCurrent() is yet null.

Thanks in advance,
David

delete flag offensive retag edit

6 Replies

Sort by ยป oldest newest

answered 2012-09-20 13:15:43 +0800

avidD gravatar image avidD
166 2

Why the hell is Sessions.getCurrent() == null in SessionListener.sessionCreated(HttpSessionEvent)? Is it not the purpose to signal that a session has been created (mind the past tense) if such a method is called?

link publish delete flag offensive edit
link publish delete flag offensive edit

answered 2012-10-02 06:14:21 +0800

avidD gravatar image avidD
166 2

Hello Jimmy,
I know that one. The problem is that there are a lot of components in our application managing session state that rely on a session existing. Each session has its own copies of resources that can be edited. All these objects are stored in a session because it must be guaranteed that every occurrence of the same resource is represented by the same object (instead of fetching a new copy from a cache or so). We cannot put all our code into the SessionInit and we don't want to hand the session down the line the database. It would really be good if there were an event that signaled "from now on it is safe to call Sessions.getCurrent()".

Regards,
David

link publish delete flag offensive edit

answered 2012-10-02 07:31:35 +0800

gekkio gravatar image gekkio flag of Finland
899 1
http://gekkio.fi/blog

Note that a ZK Session is NOT the same as HttpSession. ZK Session object is stored inside the HttpSession, so the HttpSession is always created first.

If you write code inside a HttpSessionListener, you cannot rely on the existence of a ZK Session. SessionInit is the correct interface if you need initialization code that relies on a ZK Session.

link publish delete flag offensive edit

answered 2012-10-04 06:35:04 +0800

avidD gravatar image avidD
166 2

Yes, I understand that the HttpSession is not the ZK Session. Only, from a design perspective this situation is a bit ugly for us:
Our data access layer accesses an interface that maintains the session state so as to assert that each session gets session-scope unique copies of any resource
As we use ZK, we have a ZK-based implementation that manages these copies in the ZK session.
For performance, we need to prefetch some resources from the database into the session when the session starts. However, as there seems to be no event that signals "Sessions.getCurrent() will from now return a valid session" we need to use SessionInit and set the session explicitly in our data access layer so it can use the session management interface that is independent of ZK. This is kind of counterintuitive because the session obviously already exists and there is a method that claims to return the current session.
If we had a corresponding event, we could just plainly access the required properties in a thread and no special handling of the session would be required.
So, unless I am missing something ( the said event ) I think I should file a feature request.

link publish delete flag offensive edit

answered 2012-10-04 08:57:08 +0800

gekkio gravatar image gekkio flag of Finland
899 1
http://gekkio.fi/blog

Hmm, I don't understand why SessionInit isn't the right way to implement what you want to do. If you want to prefetch stuff into the session, that's the correct way to do it.

Note that Sessions.getCurrent() in reality uses a threadlocal, which is set by ZK when request handling starts. This means that the validity of Sessions.getCurrent() is a completely separate concept from the creation of a session. Outside ZK's threads you will never be able to use Sessions.getCurrent().

Could you explain your use case with some code?

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2012-09-20 12:39:43 +0800

Seen: 174 times

Last updated: Oct 04 '12

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More