Subject Re: java.io.NotSerializableException
Author Roman Rokytskyy
Hi,

> I have embedded my Connection Objet inside a session scope Bean in
> my web application. This is done just to use the same connection
> along the complete http session.

That's the mistake.

> Should I change my code design to avoid embedding a Connection objet
> in a session scope Bean?

I do not know what the Connection Object is. Is it an instance of
java.sql.Connection class? General rule is: you cannot put
non-serializable objects into the session.

> Is every session scope bean serialized during a
> http session?

No, session is serialized when web container decides to serialize it.
(for example it was inactive for a long period of time but is still
valid, then container can decide to store it on the disk to save
server memory).

> Knowing that java.sql.Connection is not serializable, why
> my application seems to works fine most of the time?

Because web container does not serialize your session most of the
time, only occasionly.

> What's the best choice to use the same connection in a http session?

Not possible. See documentation on J2EE. You need to configure your
web container so that javax.sql.DataSource instance is accessible via
JNDI and on each request you have to lookup the data source (you can
even store it in the session), obtain the connection from the data
source and close it at the end of each request. You cannot have same
transaction for multiple requests.

Best regards,
Roman Rokytskyy