Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setAttribute: Non-serializable attribute (Java Object Serialization)

We just switched to Glassfish V2. We are now getting errors when setting a session attribute.

Error is:

java.lang.IllegalArgumentException: PWC2788: setAttribute: Non-serializable attribute

Code is:

getRequest().getSession().setAttribute("questionsForUser", getQuestions());

getQuestions() is just a simple getter which is inside an abstract class named 'Parent Action'....so to make getQuestions() a serialized object does my class need to implement serializable?:

public List getQuestions() {
    return questions;
}
  • How can we make this object serializable?
  • is it a good practice to only put serialized object in the session (as Glassfish appears to be requiring)?
  • Are there risks of sessions being swapped between users with serialized objects?

Edit: I am using ORM (iBatis)

More Information about "Questions"

setter:

public void setQuestions(List questions) {
    this.questions = questions;
}

setter is called inside this method. this method calls the iBatis mappings.

public void prepareQuestions()
{        
setExamIfaceDAO((SecurityExamIfaceDAO)ApplicationInitializer.getApplicationContext().getBean("securityExamIfaceDAO"));
    String userRole = questionsBasedOnUserRole();
    int questionsToBeShown = 0;
    if (userRole.equalsIgnoreCase("C"))
        questionsToBeShown = 15;
    else if (userRole.equalsIgnoreCase("U"))
        questionsToBeShown = 10;
    List local_questions = getExamIfaceDAO().getSecurityQuestions(userRole);
    Collections.shuffle(local_questions);
    if (local_questions.size()>=questionsToBeShown)
        setQuestions(local_questions.subList(0, questionsToBeShown));
    getRequest().getSession().setAttribute("questionsForUser", getQuestions());
}
like image 306
Omnipresent Avatar asked Nov 02 '09 16:11

Omnipresent


People also ask

How do you serialize non-serializable objects?

You can't serialise a class that doesn't implement Serializable , but you can wrap it in a class that does. To do this, you should implement readObject and writeObject on your wrapper class so you can serialise its objects in a custom way. First, make your non-serialisable field transient .

What are non-serializable objects in Java?

In Java, a NotSerializableException exception is thrown when an instance of a class must implement the Serializable interface. The exception is thrown by either the serialization runtime, or by the instance of the class. The argument for the NotSerializableException is the name of the class.

Can a serializable class contains a non-serializable field in Java?

the non-serializable field's Class must have an API to allow getting it's state (for writing to the object stream) and then instantiating a new instance with that state (when later reading from the object stream.)

What happens if object is not serializable?

What happens if you try to send non-serialized Object over network? When traversing a graph, an object may be encountered that does not support the Serializable interface. In this case the NotSerializableException will be thrown and will identify the class of the non-serializable object.


1 Answers

You can make the object serializable by using a serializable List implementation and making sure that the objects in the list are also serializable.

Yes -- it is good practice to only put serializable objects in the session since this will allow your application to be run on a server with multiple nodes. Even if you don't care about this at the moment it might be useful in future.

The servlet container should ensure that sessions are not swapped between users. Making objects stored in the session serializable allows the container to 'distribute' the session state across multiple nodes in a cluster, allowing the user who owns the session to have requests serviced by any node.

like image 90
Andy Avatar answered Sep 26 '22 16:09

Andy