"Scheiderer, Stephan" wrote:

> [snip]

>         SO, do I have to synchronize session object access??? (If a user
> makes 2 requests at one time...)
>         Does that anybody of you? Is this really necessary?
>

I will offer a few brief comments about my own practices ... your mileage may
vary.

Note that there is never any need to synchronize the session.getValue() call --
all you are doing is getting a reference to an existing object -- not modifying
anything.

In general, I try to avoid making the caller (the servlet or JSP page, in this
case) of bean methods worry about synchronization -- normally it would require
a lot of knowledge about the internals of the beans themselves to make this
decision correctly, and I want the servlet/JSP author to think of the bean as a
black box.  Any protection that is needed should be put inside the bean.

Java offers a simple approach to this, using the "synchronized" keyword.  The
language itself guarantees that no more than one code block that is
synchronized on the same object will be executed at the same time.  You can do
this by adding a "syncronized" modifier on the method name, for example:

    public synchronized void foo(String bar) {
        .....
    }

Which is pretty much the same as saying:

    public void foo(String bar) {
        synchronized (this) {
            .....
        }
    }

So, if you use the "synchronized" modifier on any methods of your bean that
need it (generally, only those that modify things), the language guarantees
that only one thread will be executing inside any one of these synchronized
methods at a time.

In general, I don't do any synchronization within the "get property" methods of
a bean, unless I'm accessing an underlying data structure that requires it.
For example, a java.util.Hashtable promises to do it's own synchronization, so
you don't have to worry about two people doing a get() or put() at the same
time and messing things up.  Objects in the JDK 1.2 "collections" API are
generatlly not synchronized, so you have to do it yourself.  For example,
assume I had a collection of User objects keyed by name in my bean, and I
wanted to return the User for the specified name.  I would code like this:

public class BeanClassName {

    TreeMap users = new TreeMap();

    public User getUser(String name) {
        synchronized (users) {
            return ((User) users.get(name));
        }
    }

    ... other methods and variables ...

}

Why didn't I just synchronize the method itself?  Because that blocks out all
calls to *any* other synchronized method.  I get better performance by locking
the "smallest" unit of stuff that I can, without stopping other threads from
calling other methods in this bean that aren't impacted by what I am doing.

There's lots more about threading and synchronization in the Java Language
Tutorial (http://java.sun.com/docs/books/tutorial), or in Computer Science
textbooks that discuss threading concepts.

Craig McClanahan

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to