If you implement the SingleThreadModel interface for your Servlet class,
TomCat will create a new instance for each request.
Albert
-Original Message-
From: kwirirai [mailto:[EMAIL PROTECTED]
Sent: Monday, February 09, 2004 2:12 PM
To: Tomcat Users List
Subject: Re: Servlet thread safety in Tomcat
But if I am to synchronise my code blocks won't that slow done the
application, or is there a "mode" that you can put Tomcat into so that
it will create a new instance
servlet thread with its own variables and execution space and stuff
like that .Because I was thinking putting all those synchronized blocks
will slow down the app.
David Ramsey wrote:
>Your primary mistake seems to be in assuming that Tomcat will create a
>new instance of a servlet for every thread started. This is not what
>happens, therefore servlet instance variables are not thread safe
>unless you take additional actions to make them safe. Likewise,
>application global entities, such as singletons, won't be thread safe
>without taking additional actions on your part. The short rule is
>simply if two threads might access this data concurrently, then you
>need to protect yourself from such a collision. About the only thing
>that is mostly thread safe are objects whose scope is limited to a
>single method, but even these can get you in trouble if you put
>references to those objects in global locations (such as an application
>wide cache).
>
>
>--- kwirirai <[EMAIL PROTECTED]> wrote:
>
>
>>The app is simply meant to grab some mails from a pop server and
>>simply
>>display it using Java Mail.
>>I realise part of my mistake is using global variables
>>,unsynchronized
>>collections and unsynchronized code blocks.I am not using the single
>>thread model.
>>My initial thought ( :-) forgive me its some time since I have coded
>>
>>servlets) was that Tomcat will create a new servlet instance that is
>>totally independed of the other, for each request.On testing the app
>>on
>>two client machines I have realized that the data is corrupted all
>>mixed
>>up and my velocity template is throwing an error caused by
>>concurrent
>>modification.
>>My question is there some way of making this app thread safe and also
>>
>>how does Tomcat actually many requests , in terms of the threading
>>method used?I am using Tomcat 4.1
>>
>>
>>Shapira, Yoav wrote:
>>
>>
>>
>>>Howdy,
>>>State your specific requirements and we can help you design
>>>servlets/objects that will meet those requirements. Your original
>>>
>>>
>>post
>>
>>
>>>is too broad to solicit a detailed response.
>>>
>>>Yoav Shapira
>>>Millennium ChemInformatics
>>>
>>>
>>>
>>>
>>>
>>>
-Original Message-
From: kwirirai [mailto:[EMAIL PROTECTED]
Sent: Monday, February 09, 2004 12:48 PM
To: [EMAIL PROTECTED]
Subject: Servlet thread safety in Tomcat
Hi All
I am developing an application that uses JavaMail.What I am
>>concered is
>>
>>
the issue of thread safety,and efficiency.My question is do I need
>>to
>>
>>
employ synchronized blocks in my Servlet code or is there another
>>way
>>
>>
>>>to
>>>
>>>
>>>
>>>
implement thread safety.I have been experimenting with the
>>application
>>
>>
and I have seen that the data is actualy mixing up.I realy need to
>>now
>>
>>
how Tomcat handles request and issues those request ,this is in
connection with threading. I have thought about the single thread
>>model
>>
>>
in my servlets but I think this is an inefficient method to use.
Thanks
Kwiri
>>>-
>>>
>>>
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail:
>>[EMAIL PROTECTED]
>>
>>
>>>
>>>
>>>This e-mail, including any attachments, is a confidential business
>>>
>>>
>>communication, and may contain information that is confidential,
>>proprietary and/or privileged. This e-mail is intended only for the
>>individual(s) to whom it is addressed, and may not be saved, copied,
>>printed, disclosed or used by anyone else. If you are not the(an)
>>intended recipient, please immediately delete this e-mail from your
>>computer system and notify the sender. Thank you.
>>
>>
>>>
>>>
>>-
>>
>>
>>>To unsubscribe, e-mail: [EMAIL PROTECTED]
>>>For additional commands, e-mail: [EMAIL PROTECTED]
>>>
>>>
>>>
>>>
>>>
>>>
>>
>>
>
>
>__
>Do you Yahoo!?
>Yahoo! Finance: Get your refund fast by filing online.
>http://taxes.yahoo.com/filing.html
>
>-
>