c# - getting db connection through singleton class -


I created a singleton class, this class gives a database connection. So my question is, is this connection also satisfactory to singleton norms?
If not, how can I make it a singleton?
Here is the code.

  Public Sealed Class Singleton DB {Fixed read-only Singleton DB Example = New Singleton DB (); Static SqlConnection con = New SqlConnection (Configuration Manager. Connection Strings ["mydb"]. ConnectionString); // To specify a clear Constructor C # compiler to type / not to be the first type of singleton ddb () Singleton DB () {} Public Static Singleton DB Instance {Receive; }} Public Stable SqlConnection GetDBConnection () {Return Co; }}  

Your singleton is still closed.

As far as the singleton pattern is gone, please see the nice and detailed description of John Skeet here:

Singleton Connection for a SQL Connection Object In fact, very bad idea is. There is no reason to do this.

If you are trying to avoid the performance hit of "new SqlConnection ()" or "connection.Open ()" then be advised that there is no display there because of the connection behind the scenes The pooling connection pooling manages the opening / closing of the expensive connection. No SqlConnection Object

You will not be able to open multiple SqlDataReaders / at the same time with the connection and thread threads will run in issues if you have multiple threads to the same connection object.

The singleton pattern is the most commonly used and abused pattern and singleton has many side effects that you do not know about. Here's a great deal about the dangers of singles


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -