Mike Engelhart wrote:
> Thanks. With session based bean passing, do you just remove the object from
> the session when you're done using it to allow the garbage collector to
> remove it from the heap when you're done? For instance, I'm working on a
> form validation bean that I want to hand off to a JSP but I want it to
> disaapear as soon as the page is displayed. I can imagine numerous sessions
> filling up with these validation beans and causing the JDK to use a lot
> resources. Also does using a session based bean imply thread safety?
>
If you remove a session-scoped bean from the session, AND you have no other
references to that bean, then the garbage collector will recycle the bean as it
needs to. To simulate the behavior of request-scope beans like validation
messages, I tend to either have my JSP page delete these objects as soon as it is
through with it, or keep re-using the same session key for the same purpose (like
using key "errorMessage" for all error messages that only need to appear once).
This effectively deletes the old value and adds the new one.
Session-scoped beans have absolutely no guarantees about thread safety.
(Request-scoped beans would be thread safe, as long as your servlets don't start
background threads that can access them.) It is entirely feasible for a client
browser to issue more than one request to servlets that are accessing the same
session simultaneously. You need to program with appropriate synchronizations
and/or locking to deal with this possibility.
Note that using SingleThreadModel on your servlets doesn't help any either -- that
just means that any one instance of the servlet won't get accessed more than once
at the same time. But the servlet engine can certainly access multiple instances
of the same servlet if you've configured them, or two different servlets that
access the same session.
Craig MdClanahan
--
--------------------------------------------------------------
To subscribe: [EMAIL PROTECTED]
To unsubscribe: [EMAIL PROTECTED]
READ THE FAQ!!!! <http://java.apache.org/faq/>
Archives and Other: <http://java.apache.org/main/mail.html/>
Problems?: [EMAIL PROTECTED]