Are JavaBean Objects - instantiated & used inside JSP ThreadSafe

Discussions

Web tier: servlets, JSP, Web frameworks: Are JavaBean Objects - instantiated & used inside JSP ThreadSafe

  1. Hi,

      Just Wanted to know if :

      JavaBean Objects - instantiated & used inside JSP
      ThreadSafe

      or

      the JavaBean Object must use the Synchronized methods.

      If anyone knows any article that talks more about this,
      I would appreciate it.

    Thanks
    Anjali
  2. Anjali,

       The answer to your question depends on the scope of the Javabeans. As you know, JavaBeans used in JSP pages can have application, session, page or request scope.

       Beans with application and session scope are subject to thread safety issues. In the case of a bean that has application scope, it is obvious that multiple threads of execution could be touching the bean simultaneously. Clearly, this warrants that the design of bean should take thread safety into account.

       Beans with session scope may appear to be thread safe, but they are not. If the client opens up multiple browser windows [Netscape and IE define sessions differently when multiple windows are opened - more headaches for developers 8( ] and starts interacting with the application then the bean that is in session scope is in jeopardy of being trampled upon by multiple threads of execution as well. Again, you need to design the bean with synchronized blocks (or any other way you see fit) to make the bean thread safe.

       As far as I know, beans with request and page scope are executed by one thread only so you should be ok.

    Hope that helps...

    Raj
  3. Raj

       That really helped me as I have beans that help in
       making the HTML Page through dynamic dropdowns. The
       beans are of Page scope. So it saves me a lot of
       headache to know that it will be executed by 1 thread.

    Thanks
    Anjali