Thx 4 the detailed example, Will read it at later on today

Daniel.

On Nov 1, 6:40 pm, Ronoaldo José de Lana Pereira
<rpere...@beneficiofacil.com.br> wrote:
> Daniel,
>
> To "convert" old session data I started to persist just the user Id in
> session. Sadly, unlike memcache, you can't control how the session is
> deserialized. In my case, any request with has the old user data in session
> was throwing 500 erros and I couldn't control this behavior (it was a
> filter from the gae...). What I did was something like this:
>
> public MyUser getCurrentUser(HttpServletRequest req) {
>    Object userOrId = req.getSession().getAttribute("user");
>    if (userOrId instanceof Long) {
>       return loadUserFromDsOrMemcache((Long) userOrId);
>    } else {
>       // Remove user from session and start storing ID for next change to
> User class dont raise exceptoins ...
>       User user = (User) userOrId;
>       req.getSession().setAttribute("user", user.getId());
>       return user;
>    }
>
> }
>
> This way I could get rid of storing this in session and of the app being
> unacessible when I changed my model (wich was happening heavly by the
> way...). Note that at the time I deployed this code, the User object hasn't
> changed.
>
> After running this for a while (you can expire sessions or delete them in
> bulk from the datastore so you can force them to expire, I deployed the new
> version.
>
> To store json data, you may use some json library that can map/convert
> to/from Java -> Json, like Google Gson:
>
> String userJson = new Gson().toJson(user);
> req.getSession().setAttribute("user", userJson);
> (...)
>
> String userJson = (String) req.getSession().getAttribute("user");
> if (userJson != null) {
>    User user = new Gson().fromJson(User.class, userJson);
>
> }
>
> This way you can add/remove fields to you model, and old session data will
> be restored as expected. Note that is a good practice to set defaults for
> the new fields, so your session data won't create unusable objects. In your
> original post, the change from string to integer may be solved by
> overriding the Gson de-serializer to handle the id field as number and not
> as string. I guess that you can change the way Java will de-serialize your
> classes too, but I'm not sure if this will solve your particular issue
> mentioned.
>
> Not sure if this is the best way to handle, but this is how I solved on our
> live app.
>
> More on Gson:http://code.google.com/p/google-gson/.
>
> Hope this helps (sorry for some typos or misspelled words)
>
> Best Regards,
>
> -Ronoaldo

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to google-appengine-java@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.

Reply via email to