Problem in serializing objects in session

S

sangha

Hi,

We are facing a problem when we try to store a excel file in session
and then retrive it from the session.

Everything works fine if we hit the URL on the server directly but when
we go via a proxy, then things fail.

-----------------------------------------------------------------------------------------------------------------
OutputStream output=new ByteArrayOutputStream();
ExcelHolder exHold = new ExcelHolder(); // This class implements
serializable interface
try {
bulkUploadId =
bulkUp.readAndValidateValues(bulkUploadForm.getUploadFile().getInputStream(),output);
}
catch (Exception e) {

exHold.setOutput(output);

request.getSession().setAttribute("ExcelErrorFile",exHold);

saveErrors(request,errors);

return mapping.findForward(bulkUploadForm.getPageRequested());
}
--------------------------------------------------------------------------------------------------

The error that we get is:

<Web application:
ServletContext(id=30100074,name=switchorder,context-path=) tried to
place a non-
serializable attribute: ExcelErrorFile into the session:
GTvL76KxG65FFsBH1w0vRrCG1jjhfTQGY8yjtSh2qcc1h51HpYbM!1750552768!4950
38327!1141829512839. This attribute will be lost upon redeployment or
session failover. Also, such attributes will be scoped
to the current server only and will not be replicated to the secondary
server. This message is logged only once per session.>


Any help will be appreciated.

Thanks,
Sangha
 
A

Ashish Pagey

Even though ExcelErrorFile is declared to implement Serializable, it
cannot be serialized because it has reference to an object of type
ByteArrayOutputStream (which is not serializable). To avoid the error
you can exclude this attribute from being serialized by declaring it as
transient:

transient private ByteArrayOutputStream out;

Note that Java serialization attempts to serialize all objects that are
referenced by the object being serialized, unless you explicitly
exclude certain references.

Error aside, it is generally not a good practice to store large objects
like files on the session. This will have negative impact on the
performance and scalability of your application. Consider storing such
large objects in a secondary store like the database.
-Ashish
 
S

sangha

Well Ashish...when i said that the ExcelHolder implements serializable,
i'am doing a
writeObject for writing the byteArrayOutputStream.

And this byteArray holds the excelsheet contents that need to be
displayed.

Well, yes it is not a good practice to store large objects in session,
but in our case we don't need to save the
exceldata. We just use the data entered in the sheet and then discard
it.
 
S

sangha

Well Ashish...when i said that the ExcelHolder implements serializable,
i'am doing a
writeObject for writing the byteArrayOutputStream.

And this byteArray holds the excelsheet contents that need to be
displayed.

Well, yes it is not a good practice to store large objects in session,
but in our case we don't need to save the
exceldata. We just use the data entered in the sheet and then discard
it.
 
A

Ashish Pagey

Something in the way you have implemented is making it
non-serializable. Without looking at the code for ExcelHolder, its hard
to say what. If you have the writeObject method implemented correctly,
you shouldn't get this error.
Check for 'silly errors' like a typo in your writeObject method
signature. The signature should look something like:

private void writeObject(OutputStream out) throws IOException

Try isolating the problem by writing a standalone program that attempts
to serialize the object:
ObjectOutputStream ois = new ObjectOutputStream( new
FileOutputStream(fileName));
ois.writeObject( excelHolder );

-Ashish
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top