Jean-Rémy Revy
Jean-Rémy Revy

Reputation: 5677

Tapestry @Persist(PersistenceConstants.FLASH) issues with Tomcat but not Jetty

I was facing an issue deploying my application based on Tapestry 5.4 (AppFuse modular one). Hopefully i solved it, but i'm still wondering why ....

Tapestry provide a simple mechanism to store variables from one page to another, serialazing the object:

package com.corp.div.project.admin;

public class EmployeList {
    /* ... */
    @Persist(PersistenceConstants.FLASH)
    ComplexObject myObject;
}

The object was correctly passed through the requests when using jetty (with mvn :etty:run), but I got an Exception when using Tomcat (6 with maven plugin, or 7 on a fresh install) :

    Error persisting field admin/EmployeList:myObject: setAttribute: Non-serializable attribute flash:admin/EmployeList::myObject

Obviously, I added implements Serialzableto my class :

public class ComplexObject implements Serializable {

/**
 * UID
 */
private static final long serialVersionUID = -76621654341617565L;

But I'm still wondering why it works with jetty and not with Tomcat...

Upvotes: 0

Views: 681

Answers (1)

lance-java
lance-java

Reputation: 27994

Under the hood, tapestry calls HttpSession.setAttribute(...) and as far as the servlet spec is concerned, this does not require the object to be Serializable.

Servlet containers will require session objects to be Serializable if they do the following

  • Save / restore sessions from disk at shutdown / startup
  • HTTPSession Clustering

By default, Tomcat saves / restores to disk whereas Jetty does not. This is why you see the issue on Tomcat but not Jetty. I'm sure that if you enabled Jetty's backup to disk (via HashSessionManager) or you enabled clustering, you would see the same sorts of exceptions on Jetty.

So, as a rule of thumb, you should always make sure that your session attributes are Serializable. Perhaps you should configure Jetty's HashSessionManager to get more consistent behaviour.

Upvotes: 1

Related Questions