Storing the User in a static field doesn't seem wise: the static field is 
shared between all session instances, so this will break.
Besides that, I'd advise not to store the User instance itself in the session, 
but only the user ID (or username, or email). The User instance can be cached 
in a transient field, which can be checked and loaded on request by a 
(non-static) getUser() method.

Something like this:

public class MySession extends WebSession {

        private int userId = -1;
        private transient User user;

        public void setUser(User user) {
                userId = user.getId();
                this.user = user;
        }

        public User getUser() {
                if (userId == -1) return null;
                if (user == null) {
                        user = UserDAO.loadUser(userId);
                }
                return user;
        }

        @Override
        public void detach() {
                user = null;
                super.detach();
        }

}


On 9 jun 2011, at 10:48, Zeldor wrote:

> Hi,
> 
> I have really serious problems with my sessions - or rather getting and
> displaying data with it. It works great when tested locally, but when
> deployed to GAE it acts weird. In bad way. I have no idea if it's wicket or
> GAE issue, but maybe I can get some hints here.
> 
> Problem is that:
> - it quite often fails to load data, so when user logs it it shows blank
> pages, without any data, that should com from session
> [MySession.loggedInUser.get...]
> - on very rare occasions it can show old values, that were changed long time
> ago, some residue must be left over somewhere and is not cleaned and somehow
> it can switch between
> - and there was a case when one user could see data from other user [so
> sessions switched?]
> 
> Of course situations 2&3 can never happen. Situation 1 neither - but maybe
> it can be solved by some check and reload of data?
> 
> Some code to illustrate:
> 
> MySession:
> 
> public class MySession extends WebSession {
>       
>       public MySession(Request request) {
>               super(request);
>       }
>       
>       public boolean isAuthenticated() {
>               return (loggedInUser != null);
>               }
>       
>       public static User loggedInUser;
>       
>               
>       public User getLoggedInUser() {
>               return loggedInUser;
>       }
>       public void setLoggedInUser(User loggedInUser) {
>               this.loggedInUser = loggedInUser;
>       }
> }
> 
> In Login:
> 
> ((MySession) MySession.get()).setLoggedInUser(user);
> 
> 
> Later data is accessed by MySession.loggedInUser.[getters/setters here] and
> persisted to datastore when anything changes.
> 
> --
> View this message in context: 
> http://apache-wicket.1842946.n4.nabble.com/Major-session-problem-GAE-tp3584894p3584894.html
> Sent from the Users forum mailing list archive at Nabble.com.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
> For additional commands, e-mail: users-h...@wicket.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org

Reply via email to