Can I store connection in Application variable?

  • Thread starter Thread starter GS
  • Start date Start date
G

GS

I have system wide connection string defined in web.config. Is it possible to store connection object in global variable accessible by whole application and get reference to it in different parts of code instead of creating new connection each time? How will this survive thread switching?
 
Boy! It just seems like there is a flood of this type of Connection - related
stuff lately!

DON'T Store Connection objects in Application state!

You can store the Connection STRING there, that's fine. Open a new
connection with this connection string just before you need to use it
anywhere in your code, and CLOSE the connection as soon as you can and allow
it to go back to the Connection Pool.

It's that simple. ADO.NET will take care of the rest.

Peter
 
Back
Top