Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post May 07, 2012 06:39 AM by Peter pi - MSFT
May 04, 2012 06:08 PM|LINK
I was recently asked to help out with an application which was crashing. The website was talking to a webservice, which was then calling the database for data. But the site would have issues when multiple users were surfing, even just 5 users would cause
it to crash. After looking through the code I saw the webservice was calling a class to get data, the class had in it static variables for the SqlConnection object as well as other variables, like static string empId. Removing the static variables and making
them local to the functions worked.
I was asked why static was the issue, I explained but wanted to confirm I got it. I told them static means 1 per application, because they were static means there was only 1 connection object that all users were trying to get, so the more users the longer
the wait and eventually the timeouts, same goes with the static string empId, all users were calling a function to use it, but they would all try to change the value of it, again because it's static they would wait to get it.
May 04, 2012 06:57 PM|LINK
For further information, have a look at this web page
May 07, 2012 06:39 AM|LINK
Static variables exist for the lifetime of the App Domain that contains them. it belongs to the class instead of each instance of this class, every quest will generate a new instance of your web service class, because each request is handled on a separete
thread, and if multiple users requests at the same time, then you will have multiple threads accessing these static variables at the same time, if more than one thread could modify the variables, then you need to interlock access to the variables so that only
one thread can modify them at a time. So you need to be careful to use static variable in a web service or ASP.NET web application.