Question on optimizing a piece of code

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello All,

I need some help on optimizing a piece of code. Currently this is how we
retrieve our Oracle's connection string for executing all the queries in our
application.

public string GetOracleClientConnectionString()
{
string strConnectionString =
System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];

return strConnectionString;
}

So each time, it has to look up in the web.config file, read the
ConnectionString and return it. Is there any way I could optimize this or
make this more efficient?

Can I store the connection string in cache or as a static variable? So if I
store this in cache would the serialization/deserialization be an overhead?

Thanks
 
Have you profiled this method and determined it to be a bottleneck? The settings
read from the config file are already cached in memory, so accessing them
shouldn't be a concern.

-Brock
DevelopMentor
http://staff.develop.com/ballen
 
private static strConnectionString = null;
public string GetOracleClientConnectionString()
{
if (strConnectionString == null)
strConnectionString =
System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
return strConnectionString;
}

note: you have a minor race condition at startup (may set the string more
than once), but the locking overhead to prevent is probably not worth it.

-- bruce (sqlwork.com)
 
Test first to see if it's a problem. I remember to have seen that web.config
is cached anyway.
Your best bet is likely to check first where the time budget is spent...

Patrice
 

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

Back
Top