Application.Lock()

A

A.M

Hi,

I have following code inside Global.asax.cs. It decrypts the connection
string and assigns it to m_strConnectionString. subsequent instances of
application use the same value.
I am wondering that can this property cause concurrency problems?
Should I Lock and Unlock application object when i am assigning new value to
m_strConnectionString?

Thanks,
Ali


public class Global : System.Web.HttpApplication
{

public static string m_strConnectionString = string.Empty;

...............
.............

public static string ConnectionString
{
get
{
try
{
if (m_strConnectionString == string.Empty )
{
m_strConnectionString = GetEncryptedAppSettings("ConnStr").ToString ();
}

}
catch(Exception e)
{
// FatalError !! Kill the app
}
return m_strConnectionString ;
}
}
 
A

Abhijeet Dev

I'd have preferred to do so on application start - just add your connection
string to the application object on start ?
 
A

Alvin Bruney [MVP]

bad idea. Application.Lock locks the application object and forces requests
to queue up. this introduces delay into the pipeline. very bad.
 
A

A.M

So my code won't have concurency problem?

Thanks,
Ali

Alvin Bruney said:
bad idea. Application.Lock locks the application object and forces requests
to queue up. this introduces delay into the pipeline. very bad.

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
A.M said:
Hi,

I have following code inside Global.asax.cs. It decrypts the connection
string and assigns it to m_strConnectionString. subsequent instances of
application use the same value.
I am wondering that can this property cause concurrency problems?
Should I Lock and Unlock application object when i am assigning new
value
to
m_strConnectionString?

Thanks,
Ali


public class Global : System.Web.HttpApplication
{

public static string m_strConnectionString = string.Empty;

..............
............

public static string ConnectionString
{
get
{
try
{
if (m_strConnectionString == string.Empty )
{
m_strConnectionString = GetEncryptedAppSettings("ConnStr").ToString ();
}

}
catch(Exception e)
{
// FatalError !! Kill the app
}
return m_strConnectionString ;
}
}
 
S

Steven Cheng[MSFT]

Hi Ali,

From the code you provide, you use the following code
GetEncryptedAppSettings("ConnStr").ToString ();
to retrieve the connection string everytime the static global property is
requested, I'm not sure whether this data in your application will
frequently changes, but I do think it'll cause potential concurrence
problem and also the performance won't be good.

I think you may consider Abhijeet's suggestion that set the static member
only at the application's start event such as
protected void Application_Start(Object sender, EventArgs e)
{
//set the static member
}

then, you needn't retrieve the connectionstring from configuration
everytime the property is requested. Do you think so?
If you have any other ideas or questions, please feel free to post here.
Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top