Remove attribute from session before serialize

jakarta-ee, java, session

Solution

You can make a second class that wraps your first non-serializable into an object reference, and mark it as `transient`:

public class Wrapper implements Serializable
{
    public transient YourClass obj;
}

`transient` variable won't be serialized upon serialization, and it will be assigned to `null` after deserializarion of `Wrapper` object.

Problem

I saved some references in user sessions that link to objects which aren't serializable. I would like to remove these attributes from the session before app shut down, before session serialization. Is there a way to do so? I already tried with a Listener that listens to app destroy, but sessions are already invalidated at that moment.

Original source