Another question about threading. I think this is something that definitely
needs to go into the FAQ.
Mirko Wolf wrote:
>
> Hi there,
>
> Here is another question how to design functions for a servlet.
> IMHO for every request made to a servlet the service function is called again.
Yes, service is called every time. It is called by the server. The server uses a
thread to call the servlet's methods.
> But what happens, when a critical function is called by two or more service
>functions at the same time?
Each thread executes the code.
> For instance a function which handles a database connection and the belonging
>queries.
> What happens, when a second service function call this method while it handle's a
>database querie for the first one?
> The first one will have no chance to complete the request, right?
It depends. If you wrote your servlet to be thread-safe, then each thread will
be able to complete the request correctly. However, let's say you use a static
variable to hold the database connection. Since each thread uses the same
connection to query the database, then it is entirely possible that the second
request will complete and the first won't; it's also possible that the opposite
will happen, or that both will complete correctly. It is most likely that if you
have concurrent requests to the servlet, one or both will fail.
> Should the database function synchronized or is it better to implement the
>SingleThreadModel?
> I think it is easier to implement the SingleThreadModel, because i've a lot of
>functions the must be synchronized and
> then i'am afraid to have a deadlock in the system.
> Any comments or suggestions?
>
> Mirko
In order of my preference, here are some of the ways to make your servlet
thread-safe:
1. Use local variables for any data that is changed by a thread. Use instance
variables only for data that is immutable. For data base queries, create a new
connection every time or (the better solution) use a connection pool and get a
connection object from the pool.
2. If you must use instance variables that are changed by a servlet method, then
use synchronized blocks of code.
3. Use synchronized methods.
4. Use the SingleThreadModel. I believe this solution to be the least desirable,
because creating new instances of a servlet to handle concurrent requests is
more expensive than creating new threads to handle concurrent requests.
Note that you do not need to implement any threading code in your servlet, your
servlet does not need to extend Thread or implement Runnable. Threading is
handled by the Web Server or Servlet Server. You just need to ensure your code
is thread-safe.
For more information about threads and servlets, see Hunter, Jason (1998), "Java
Servlet Programming," O'Reilly and Associates, Chapter 3
For more information about threads, see Oaks, Scott and Wong, Henry (1997),
"Java Threads," O'Reilly and Associates
Kevin Mukhar
___________________________________________________________________________
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