RE: Servlet thread safety in Tomcat
You could try jmeter http://jakarta.apache.org/jmeter/index.html -Original Message- From: kwirirai [mailto:[EMAIL PROTECTED] Sent: 11 February 2004 17:32 To: Tomcat Users List Subject: Re: Servlet thread safety in Tomcat Thanks to All for your help ! :-) Sofar it seems to be working ,no data corruption,I localized all variables ,put in some synchronized blocks,removed them again(synchronized blocks) and seems to work, but not sure why without the synchrozed blocks its working . :-\ Is there free software that I can use to test the concurrency issues,because at the moment I am trying to test all this using two client machines and this does not work at all.Software that can create many simultanoeus connections sending in the request parameters to Tomcat would do well for testing . Shapira, Yoav wrote: >Howdy, > > > >>this has made me think a lot about threading , it is really complicated >>, I have researched about it but never found a clean solution to >> >> > >Threading is complicated, yes, and difficult to do well. Which is why >when possible you should let someone else do the work for you and use a >library like Doug Lea's util.concurrent, which is now >java.util.concurrent in JDK 1.5. It has thread pools, executors, locks, >etc so you don't have to write any of this sync code yourself. > >Yoav Shapira > > > > >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] > > > > - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Servlet thread safety in Tomcat
Mike Curwen wrote: 1) if by 'localized' you mean "I've moved the variables from outside the doGet()/doPost() methods, to inside those methods"... then this is why there is no 'data corruption' (due to multithreading issues), and it's why you don't require synchronized access to those variables. You definitely do not need synchronized on local variables. However, you DO need it if you use something "shared" among the requests. That is, outside the local scope. I will probably explain this perhaps not 100% correctly, but someone will catch me when I fall.. ;) A user request = one java thread. If more than one user requests something from your servlet, that means (potentially) more than one thread in your servlet methods. If one thread alters a variable outside of the doGet()/doPost() methods, then this modifies that variable for ALL threads, and thus, you get data confusion/corruption. It's a simple matter of scope. If the thread alters a variable within the method, then this variable is located in the method 'stack', and altering its value will only alter the data for that particular thread. So it is literally impossible to 'confuse' the data, if all of your variables are declared within the method. Again, a matter of scope. No objection ;-) 2) jmeter is an open-source web-testing tool. Fairly simple, and works quite nicely. You can use it to load test an application, and can configure it to launch X threads simulatenously. This would give you a fair chance of testing concurrency issues. 'X' will be solely determined by how much CPU and memory your testing box has. Jmeter lets you slave multiple workstations to use as load generators, so X can become quite high (which is good). You can also configure jmeter to check the results of a particular web request. So for example, if a user submits a search to search.jsp with a certain set of search values... and you expect a result in your page of : your search returned 500 results Then you can actually test for this string in the returned webpage. If the '500' is something else (say '450'), then you can begin to suspect concurrency issues. (Because perhaps another of your simultaneous users used a different set of parameters, say ones that would produce '450'). It will not be easy to pinpoint concurrency problems using JMeter. The procedure is roughly what Mike described. However, you will need to know your data very well, and possibly output some debugging info in your pages (maybe inside a HTML comment). It will not be easy. From my experience, JMeter is really great for cases where border conditions make the server fail with a 500. You stress it, and it is more likely to fail. But detecting "slight" errors in the information contained in a page is a different business. I wish I could give you an alternative, but I am afraid I know none. Yours, Antonio Fiol smime.p7s Description: S/MIME Cryptographic Signature
RE: Servlet thread safety in Tomcat
1) if by 'localized' you mean "I've moved the variables from outside the doGet()/doPost() methods, to inside those methods"... then this is why there is no 'data corruption' (due to multithreading issues), and it's why you don't require synchronized access to those variables. I will probably explain this perhaps not 100% correctly, but someone will catch me when I fall.. ;) A user request = one java thread. If more than one user requests something from your servlet, that means (potentially) more than one thread in your servlet methods. If one thread alters a variable outside of the doGet()/doPost() methods, then this modifies that variable for ALL threads, and thus, you get data confusion/corruption. It's a simple matter of scope. If the thread alters a variable within the method, then this variable is located in the method 'stack', and altering its value will only alter the data for that particular thread. So it is literally impossible to 'confuse' the data, if all of your variables are declared within the method. Again, a matter of scope. 2) jmeter is an open-source web-testing tool. Fairly simple, and works quite nicely. You can use it to load test an application, and can configure it to launch X threads simulatenously. This would give you a fair chance of testing concurrency issues. 'X' will be solely determined by how much CPU and memory your testing box has. Jmeter lets you slave multiple workstations to use as load generators, so X can become quite high (which is good). You can also configure jmeter to check the results of a particular web request. So for example, if a user submits a search to search.jsp with a certain set of search values... and you expect a result in your page of : your search returned 500 results Then you can actually test for this string in the returned webpage. If the '500' is something else (say '450'), then you can begin to suspect concurrency issues. (Because perhaps another of your simultaneous users used a different set of parameters, say ones that would produce '450'). > -Original Message- > From: kwirirai [mailto:[EMAIL PROTECTED] > Sent: Wednesday, February 11, 2004 11:32 AM > To: Tomcat Users List > Subject: Re: Servlet thread safety in Tomcat > > > Thanks to All for your help ! :-) > Sofar it seems to be working ,no data corruption,I localized all > variables ,put in some synchronized blocks,removed them > again(synchronized blocks) and seems to work, but not sure > why without > the synchrozed blocks its working . :-\ > Is there free software that I can use to test the concurrency > issues,because at the moment I am trying to test all this using two > client machines and this does not work at all.Software that > can create > many simultanoeus connections sending in the request parameters to > Tomcat would do well for testing > . > > > > > Shapira, Yoav wrote: > > >Howdy, > > > > > > > >>this has made me think a lot about threading , it is really > >>complicated , I have researched about it but never found a clean > >>solution to > >> > >> > > > >Threading is complicated, yes, and difficult to do well. > Which is why > >when possible you should let someone else do the work for > you and use a > >library like Doug Lea's util.concurrent, which is now > >java.util.concurrent in JDK 1.5. It has thread pools, executors, > >locks, etc so you don't have to write any of this sync code yourself. > > > >Yoav Shapira > > > > > > > > > >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] > > > > > > > > > > - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Servlet thread safety in Tomcat
Thanks to All for your help ! :-) Sofar it seems to be working ,no data corruption,I localized all variables ,put in some synchronized blocks,removed them again(synchronized blocks) and seems to work, but not sure why without the synchrozed blocks its working . :-\ Is there free software that I can use to test the concurrency issues,because at the moment I am trying to test all this using two client machines and this does not work at all.Software that can create many simultanoeus connections sending in the request parameters to Tomcat would do well for testing . Shapira, Yoav wrote: Howdy, this has made me think a lot about threading , it is really complicated , I have researched about it but never found a clean solution to Threading is complicated, yes, and difficult to do well. Which is why when possible you should let someone else do the work for you and use a library like Doug Lea's util.concurrent, which is now java.util.concurrent in JDK 1.5. It has thread pools, executors, locks, etc so you don't have to write any of this sync code yourself. Yoav Shapira 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]
RE: Servlet thread safety in Tomcat
Howdy, >this has made me think a lot about threading , it is really complicated >, I have researched about it but never found a clean solution to Threading is complicated, yes, and difficult to do well. Which is why when possible you should let someone else do the work for you and use a library like Doug Lea's util.concurrent, which is now java.util.concurrent in JDK 1.5. It has thread pools, executors, locks, etc so you don't have to write any of this sync code yourself. Yoav Shapira 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]
Re: Servlet thread safety in Tomcat
Thanks very much for your contribution guys! :-) This has given me new approaches to the problem.I intend to remodify,try the synchronization and do the testing/benchmarking.But this has made me think a lot about threading , it is really complicated , I have researched about it but never found a clean solution to multithreading in my apps.Initaly I thought with servlets I dont have to wory about the threading issue, now I know its everywhere. I realy have todo a lot of testing. Shapira, Yoav wrote: Howdy, Synchronization does produce overhead, but it's what you *must* do if you will not re-write your servlets to no longer contain instance fields, *and* you wish to provide thread safety. Well-put. And you might be pleasantly surprised at how small the overhead is. Which is why I always suggest testing/benchmarking ;) There is a mode, and you have mentioned it before.. It's not a Tomcat mode, it's that "Single threaded" thing. (see, I can't even write it correctly, I don't even know what the exact name is). It's SingleThreadModel, which is deprecated (with no replacement) in the latest version of the servlet specification. So even if you're not using it already, don't start now. If the request that wants the 300 emails is taking "too long" (as decided by the container), then that user's request thread is suspended, while the other user's requests are handled. That's the whole point of multi-threading the servlet... (so that requests don't pile up)? That's not done by tomcat: all requests have equal priority from the Tomcat point of view. But the OS and JVM implement preemptive multitasking, and will split CPU time between the threads. So you're right Mike, if request A takes a very long time, and request B starts after request A, request B does NOT have to wait until request A is done to get CPU time. Yoav Shapira 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]
Re: Servlet thread safety in Tomcat
Another approach is to have your application create an instance for each session that will retrieve the mail for that session. Just ensure that the resource you are accessing can handle it, or you will have problems either way. (Note I am approaching this from a programming view. I am not familar with JavaMail yet.) Not much different than what Albert proposes, just changes where you split the tree in the process. Doug Parsons www.parsonstechnical.com - Original Message - From: "Yu, Albert" <[EMAIL PROTECTED]> To: "'Tomcat Users List'" <[EMAIL PROTECTED]> Sent: Monday, February 09, 2004 2:23 PM Subject: RE: Servlet thread safety in Tomcat > > 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 > >&
RE: Servlet thread safety in Tomcat
Howdy, >Synchronization does produce overhead, but it's what you *must* do if >you will not re-write your servlets to no longer contain instance >fields, *and* you wish to provide thread safety. Well-put. And you might be pleasantly surprised at how small the overhead is. Which is why I always suggest testing/benchmarking ;) >There is a mode, and you have mentioned it before.. It's not a Tomcat >mode, it's that "Single threaded" thing. (see, I can't even write it >correctly, I don't even know what the exact name is). It's SingleThreadModel, which is deprecated (with no replacement) in the latest version of the servlet specification. So even if you're not using it already, don't start now. >If the request that wants the 300 emails is taking "too long" (as >decided by the container), then that user's request thread is suspended, >while the other user's requests are handled. That's the whole point of >multi-threading the servlet... (so that requests don't pile up)? That's not done by tomcat: all requests have equal priority from the Tomcat point of view. But the OS and JVM implement preemptive multitasking, and will split CPU time between the threads. So you're right Mike, if request A takes a very long time, and request B starts after request A, request B does NOT have to wait until request A is done to get CPU time. Yoav Shapira 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]
RE: Servlet thread safety in Tomcat
Synchronization does produce overhead, but it's what you *must* do if you will not re-write your servlets to no longer contain instance fields, *and* you wish to provide thread safety. There is a mode, and you have mentioned it before.. It's not a Tomcat mode, it's that "Single threaded" thing. (see, I can't even write it correctly, I don't even know what the exact name is). However, the decision to go with 'single threaded' will mostly be met with howls of derision and scorn... many people (correctly) believe this to be a poor idea. However, since you're already done a lot of work, and you're not willing to re-write.. it's the option that you're left with. Another topic you seem worried about is that one user requesting his 300 emails will cause other people (perhaps with only 10 or 20 emails) to wait. I'm perhaps completely off my rocker, but I think this is handled by having servlet multi-threaded in the first place. If the request that wants the 300 emails is taking "too long" (as decided by the container), then that user's request thread is suspended, while the other user's requests are handled. That's the whole point of multi-threading the servlet... (so that requests don't pile up)? Yoav.. is that completely wrong? > -Original Message- > From: kwirirai [mailto:[EMAIL PROTECTED] > Sent: Monday, February 09, 2004 1: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: Servl
RE: Servlet thread safety in Tomcat
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 >>>> >>>> >>>> >>>> >>>> >>>> >>>- >>> >>&
RE: Servlet thread safety in Tomcat
Howdy, >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. Seeing as how adding a synchronized block is easy, why don't you test it? You might find that the performance is satisfactory ;) Yoav Shapira 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]
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 - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Servlet thread safety in Tomcat
snipet for (int i = msgCount; i >=stopPoint; i--) { m = folder.getMessage(i); //get the message } This actually hapening when one session is downloading suppose 300 emails, and the other lets say 50.This suppose ,what I am actually doing is I have two machines , I first logon one machine and immediatly i log on to the other.( to simulate simultanous process). I do understand about Servlet sessions and Javamail sessions. I was actualy thinking ,somehow there should be better way.. Shapira, Yoav wrote: Howdy, Thanks ,It realy means I have to redesign the whole app ,but there is the issue of networking and Java Mail, it seems the other servlet has to wait for a very long time for the other servlet to get all the mail.My question is can Tomcat process two sessions of a servlet downloading mail at the same time sopposing the other session is slow.Will the one with a faster connection finish first or will have to wait for the one with slow connection...? This is specific to your app. What code are you executing to "download mail"? Please also be sure you're not confusion a JavaMail "session" with a Servlet "session." The two are entirely different, despite sharing the name "session." In general, it is undesirable for one servlet to wait for another. That's too high a degree of coupling. Yoav Shapira 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]
RE: Servlet thread safety in Tomcat
Howdy, >Thanks ,It realy means I have to redesign the whole app ,but there >is the issue of networking and Java Mail, it seems the other servlet has >to wait for a very long time for the other servlet to get all the >mail.My question is can Tomcat process two sessions of a servlet >downloading mail at the same time sopposing the other session is >slow.Will the one with a faster connection finish first or will have to >wait for the one with slow connection...? This is specific to your app. What code are you executing to "download mail"? Please also be sure you're not confusion a JavaMail "session" with a Servlet "session." The two are entirely different, despite sharing the name "session." In general, it is undesirable for one servlet to wait for another. That's too high a degree of coupling. Yoav Shapira 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]
RE: Servlet thread safety in Tomcat
Howdy, >But how does Tomcat log the sessions variables PER request then? Is the >session object independent of the servlet object, and is there one >session object PER request? The session object is independent of the servlet object. There is NOT one session per request (unless the user's session happens to consist of a single request, but that's an unusual border case). I suggest you read the Servlet Specification to see how sessions are defined and managed by the container. If you feel like looking at the relevant tomcat source, ManagerBase is a decent place to start, here: http://cvs.apache.org/viewcvs.cgi/jakarta-tomcat-catalina/catalina/src/s hare/org/apache/catalina/session/ and look for classes in the code that use the Manager interface, call createSession(), etc. Yoav Shapira 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]
Re: Servlet thread safety in Tomcat
Thanks ,It realy means I have to redesign the whole app ,but there is the issue of networking and Java Mail, it seems the other servlet has to wait for a very long time for the other servlet to get all the mail.My question is can Tomcat process two sessions of a servlet downloading mail at the same time sopposing the other session is slow.Will the one with a faster connection finish first or will have to wait for the one with slow connection...? Mike Curwen wrote: The one thing that could be said (so far), is to ensure you are using properly scoped variables. Generally speaking, for servlets, that means don't use something like the following: public class myservlet extends HttpServlet { private String MyBadlyPlacedVariable = null; public void doGet() { // exceptions and parameters snipped for brevity MyBadlyPlacedVariable = request.getParameter("some_variable_used_later"); // later on in the code, use "MyBadlyPlacedVariable"... // it probably contains a value you're not expecting! } } You'll for sure experince threading problems, and this is the most likely cause of any class of problems that's generically described as: "the data is actualy mixing up". -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] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Servlet thread safety in Tomcat
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 - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Servlet thread safety in Tomcat
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. Tomcat will not create a new instance of a servlet for every request to that servlet. Tomcat will create one instance on startup if the servlet is defined in web.xml as such (a load-on-startup tag present). Tomcat may create more instances as needed to serve the request load. But how does Tomcat log the sessions variables PER request then? Is the session object independent of the servlet object, and is there one session object PER request? Cheers S Yoav Shapira 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] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Servlet thread safety in Tomcat
Howdy, >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. Tomcat will not create a new instance of a servlet for every request to that servlet. Tomcat will create one instance on startup if the servlet is defined in web.xml as such (a load-on-startup tag present). Tomcat may create more instances as needed to serve the request load. Yoav Shapira 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]
RE: Servlet thread safety in Tomcat
The one thing that could be said (so far), is to ensure you are using properly scoped variables. Generally speaking, for servlets, that means don't use something like the following: public class myservlet extends HttpServlet { private String MyBadlyPlacedVariable = null; public void doGet() { // exceptions and parameters snipped for brevity MyBadlyPlacedVariable = request.getParameter("some_variable_used_later"); // later on in the code, use "MyBadlyPlacedVariable"... // it probably contains a value you're not expecting! } } You'll for sure experince threading problems, and this is the most likely cause of any class of problems that's generically described as: "the data is actualy mixing up". > > > >-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] > - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Servlet thread safety in Tomcat
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]
RE: Servlet thread safety in Tomcat
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]