What happen if I use static member in asp.net?

  • Thread starter Thread starter Yok
  • Start date Start date
Yok said:
Can I create a singleton class and put a SqlConnection in it and keep it
open?

In theory, yes you can. In practice, you should not do this.

If you keep the SqlConnection object around across requests, you will
effectively prevent ADO.NET pooling from doing its job. The recommended
usage pattern with an SqlConnection object is to allocate it, use it and
Close it within the same request.

Another problem with keeping an SqlConnection around in a static member is
that the instance members of SqlConnection are not guaranteed to be thread
safe.

In short, not a good idea.

Sami
 
Back
Top