• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1395
  • Last Modified:

Synchronize doPost, doGet?

Hello,

Could anyone advise if the doGet, and doPost methods should be declared as Synchronized in the servlet classes to ensure the servlets are use efficently?

Thanks
0
EmailSurfer
Asked:
EmailSurfer
1 Solution
 
fargoCommented:
Hi,

I would do the following in a servlet to acheive synchronisation

 /**
     * @param arg0
     * @param arg1
     * @throws javax.servlet.ServletException
     * @throws java.io.IOException
     */
    protected void doGet(
        HttpServletRequest request,
        HttpServletResponse response)
        throws ServletException, IOException
    {
        synchronized (this)
        {
                    servletBodyCodeOrMethod(request, response);
        }
    }

    /**
     * @param arg0
     * @param arg1
     * @throws javax.servlet.ServletException
     * @throws java.io.IOException
     */
    protected void doPost(
        HttpServletRequest request,
        HttpServletResponse response)
        throws ServletException, IOException
    {
        doGet(request, response);
    }

Hope it helps.
fargo
0
 
EmailSurferAuthor Commented:
Thanks

In my J2EE books the examples which follow a MCV pattern, do not use the synchronized keyword in the servlets.

Do you think most people donot use synchronized in servlets, or are there particular only circumstances where we would use the synchronized keyword in a servlet?
0
 
fargoCommented:
Hi,

There is a very good explaination about synchronize or thread-safe servlets here
http://www.jguru.com/faq/view.jsp?EID=150

the above approach i mentioned is quite common and generally it is also preferred to do synchronize on the shared object level. In books u may find this as a matter of lines and not as a topic. But using synchronize in servlets is not new!

regards,
fargo
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
hoomanvCommented:
synchronizing on "this" is not a good idea, your servlet wont be able to response to more than one request at a time, its not even a way to make your servlet run efficiently. what do you mean of efficient ?
0
 
fargoCommented:
hi hoomanv, what do u think could be any other way to do synchronise if not on "this". I agree and i already stated that the share object level sync is more better. I never said it is efficient, but depends upon context it is useful. The link i posted also clears this topic.

fargo
0
 
hoomanvCommented:
yes there is other way. your servlet needs to implement the empty interface javax.servlet.SingleThreadModel and nothing more is required. this will guarantee that no two threads will simultaneously execute your servlet's service() method, by nstantiating a new servlet for each simultaneous request. thus it is not and efficient way either.
I think you have to do proper OO design before attempting to use whether synchronization or SingleThreadModel. and where ever your thread needs to be safe, its better to synchronize on an local variable (simply called a mutex or mutual exclusion that is shared among all threads) not on the entire servlet instance. accurately identify which code block truly needs to be synchronized as a small synchronize block would be more specific to what you want to do. and at last if your load time minimal in the case that your synchronized code is more than nonsynchronized you have the choice to use SingleThreadModel and take the responsiblity.
0
 
TimYatesCommented:
>> you have the choice to use SingleThreadModel and take the responsiblity.

Since 2.4 of the Servlet spec, SingleThreadModel is deprecated, so it's recommended you handle synchronization yourself
0
 
fargoCommented:
Hi,

SingleThreadModel can not guarantee thread safety in case of session variables and static variables. In fact SingleThreadModel is deprecated from Servlet 2.4 onwards. So as you yourself stated, proper OO design is the way to go...and the way i said is just a case of partial synchronisation. Best way is to avoid the instance variables in the servlet.

have a look here
http://www.javaworld.com/javaworld/jw-07-2004/jw-0712-threadsafe.html

the SingleThreadModel is definitely not a way to go!

fargo
0
 
fargoCommented:
Sorry Tim, i posted the same line. Didn't refresh the page.

fargo
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now