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.
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.
pdassnyc
Member
221 Points
228 Posts
static variables, want to understand this..
May 04, 2012 06:08 PM|LINK
Hi,
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.
Thanks.
Gaspard
Contributor
2066 Points
416 Posts
Re: static variables, want to understand this..
May 04, 2012 06:57 PM|LINK
For further information, have a look at this web page
http://blogs.iis.net/brian-murphy-booth/archive/2007/06/15/static-shared-or-not-who-cares.aspx
Regards
Peter pi - M...
Star
12871 Points
1786 Posts
Re: static variables, want to understand this..
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.
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework