Collette Peter wrote:
> thanks for answering my question Jari.
>
> I'm not an expert in Java and JSP, so I have more questions.
>
> Session beans should be kept thread safe because a user can open multiple
> browser session. Is the 'synchronized (this)' approach the only solution ? And
> the situation of using frames with several requests participating the same
> session objects. Here also 'synchronized(this) ?
>
Using "synchronized(this)" blocks (or the equivalent, which is declaring a method
to be synchronized) is certainly one weapon you can use. However, it is sometimes
a little too broad, because it blocks simultaneous access to all threads that
encounter such a block.
What I've often found is that a bean has properties that can be modified
independently, and which don't directly impact each other. In such cases, I'd
rather protect each object wih synchronization, rather than the bean itself --
something like this:
public class MyBean {
MyFoo foo = new MyFoo(...);
MyBar bar = new MyBar(...);
public void modifyFoo(...) {
synchronized (foo) {
... modify foo's properties ...
}
}
public void modifyBar(...) {
synchronized (bar) {
... modify bar's properties ...
}
}
}
That way, two different threads can be modifying foo and bar at the same time,
without interference. This only works if the changes are independent of each other
(a classic case is where "foo" and "bar" are instances of the JDK 1.2 collection
classes).
Note that, depending on your application, you might need to synchronize readers as
well as writers. For example, if you're making a series of related changes in your
modify routine, you probably want to synchronize a getSomethingFromFoo() method as
well, so it does not see a partially completed transaction. But you'd still do it
on the underlying internal objects, so that a getSomethingFromFoo() and
getSomethingFromBar() call could happen simultaneously.
Craig
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html