-
FEATURED COMPONENTS
First time here? Check out the FAQ!
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
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?
HI avidD
You can add this listener
http://books.zkoss.org/wiki/ZK_Configuration_Reference/zk.xml/The_listener_Element/The_org.zkoss.zk.ui.util.SessionInit_interface
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
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.
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.
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?
Asked: 2012-09-20 12:39:43 +0800
Seen: 174 times
Last updated: Oct 04 '12