通过单子类获得数据库连接[英] getting db connection through singleton class

本文是小编为大家收集整理的关于通过单子类获得数据库连接的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我创建了一个单例类,此类返回数据库连接.所以我的问题是这种连接也满足了单身标准吗?
如果没有,那么我如何使它成为单身人.
这是代码.

public sealed class SingletonDB
{
    static readonly SingletonDB instance = new SingletonDB();
    static SqlConnection con =new SqlConnection(ConfigurationManager.ConnectionStrings["mydb"].ConnectionString);

    // Explicit static constructor to tell C# compiler
    // not to mark type as beforefieldinit
    static SingletonDB()
    {
    }

    SingletonDB()
    {
    }

    public static SingletonDB Instance
    {
        get
        {
            return instance;
        }
    }

    public static SqlConnection GetDBConnection()
    {
        return con;
    }
}

推荐答案

您的单身人士仍然关闭.

就Singleton图案而言,请在此处查看Jon Skeet的非常好的详细说明: http://www.yoda.arachsys.com/csharp/singleton.html

使用单身顿进行SQLConnection对象是一个非常非常糟糕的主意.没有理由这样做.

如果您试图避免" new SQLConnection()"或" Connection.open()"的性能命中,请建议在幕后进行连接池,因此确实没有性能.连接池处理 昂贵 连接的开口/关闭.不是sqlConnection对象.

您将无法同时打开多个SQLDATAREADER/命令,如果您尝试与多个线程共享相同的连接对象,则将遇到线程阻止问题.

单例模式是最使用和滥用的模式,单身人士的副作用很多,您可能不知道.很好地谈论Singletons的危险 http://www.youtube.com/watch?v = -frm3vphsei

其他推荐答案

in .net c#您可以像这样

wrtie singleton
    public class Singleton{
public static readonly Singleton Instance= new Singleton();
private Singleton(){}

或用于多线程环境:

using System;

public sealed class Singleton
{
   private static volatile Singleton instance;
   private static object syncRoot = new Object();

   private Singleton() {}

   public static Singleton Instance
   {
      get 
      {
         if (instance == null) 
         {
            lock (syncRoot) 
            {
               if (instance == null) 
                  instance = new Singleton();
            }
         }

         return instance;
      }
   }
}

其他推荐答案

连接本身不满足单例标准,因为您可以创建数据库连接对象的多个实例.根据定义,单身人士只能实例化一次.

you can 通过将您的示例更改为

,使SqlConnection成为单身人士的一部分
public sealed class SingletonDB
{
    private static readonly SingletonDB instance = new SingletonDB();
    private readonly SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["mydb"].ConnectionString);

    // Explicit static constructor to tell C# compiler
    // not to mark type as beforefieldinit
    static SingletonDB()
    {
    }

    private SingletonDB()
    {
    }

    public static SingletonDB Instance
    {
        get
        {
            return instance;
        }
    }

    public SqlConnection GetDBConnection()
    {
        return con;
    }
}

这样的方式,您的SqlConnection SingletonDB类使用的SqlConnection将只有一个SqlConnection,因此请按照单身模式.

本文地址:https://www.itbaoku.cn/post/627685.html

问题描述

I have created an singleton class, this class returns a database connection. So my question is this connection is also satisfying singleton criteria?
If no, than how I can make it singleton.
Here is the code.

public sealed class SingletonDB
{
    static readonly SingletonDB instance = new SingletonDB();
    static SqlConnection con =new SqlConnection(ConfigurationManager.ConnectionStrings["mydb"].ConnectionString);

    // Explicit static constructor to tell C# compiler
    // not to mark type as beforefieldinit
    static SingletonDB()
    {
    }

    SingletonDB()
    {
    }

    public static SingletonDB Instance
    {
        get
        {
            return instance;
        }
    }

    public static SqlConnection GetDBConnection()
    {
        return con;
    }
}

推荐答案

Your Singleton is still off.

As far as the singleton pattern goes, please see Jon Skeet's very good and detailed description here : http://www.yoda.arachsys.com/csharp/singleton.html

Using a Singleton for a SqlConnection object is a really, really bad idea. There is no reason to do this whatsoever.

If you are attempting to avoid a performance hit of "new SqlConnection()" or "connection.Open()" be advised that there really is no performance hit there because of the connection pooling going on behind the scenes. Connection Pooling handles the opening/closing of the expensive connections. Not the SqlConnection object.

You won't be able to open multiple SqlDataReaders/Commands with the connection at the same time and will run into thread blocking issues if you are trying to share the same connection object with multiple threads.

The Singleton pattern is the most over used and abused pattern and there are many side effects of the singleton that you may not be aware of. Very good talk about the dangers of singletons here http://www.youtube.com/watch?v=-FRm3VPhseI

其他推荐答案

In .NET C# you can wrtie your singleton like this

    public class Singleton{
public static readonly Singleton Instance= new Singleton();
private Singleton(){}

or for multi threaded environment:

using System;

public sealed class Singleton
{
   private static volatile Singleton instance;
   private static object syncRoot = new Object();

   private Singleton() {}

   public static Singleton Instance
   {
      get 
      {
         if (instance == null) 
         {
            lock (syncRoot) 
            {
               if (instance == null) 
                  instance = new Singleton();
            }
         }

         return instance;
      }
   }
}

其他推荐答案

The connection itself is not satisfying the Singleton criteria because you can create multiple instances of a database connection object. A singleton by definition can only be instantiated once.

You can make the SqlConnection a part of the Singleton, by changing your example to this:

public sealed class SingletonDB
{
    private static readonly SingletonDB instance = new SingletonDB();
    private readonly SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["mydb"].ConnectionString);

    // Explicit static constructor to tell C# compiler
    // not to mark type as beforefieldinit
    static SingletonDB()
    {
    }

    private SingletonDB()
    {
    }

    public static SingletonDB Instance
    {
        get
        {
            return instance;
        }
    }

    public SqlConnection GetDBConnection()
    {
        return con;
    }
}

This way the SqlConnection used by your SingletonDB class would have one and only one SqlConnection, thus follow the Singleton pattern.