程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> 數據庫連接池Data Connection Framework

數據庫連接池Data Connection Framework

編輯:關於.NET

數據庫連接池Data Connection Framework具備Connection pooling機制,能管理不同的ADO.net Connection。並通過池對象工廠去PoolObjectFactory去創建數據連接池。(可參考:對象工廠創建--- Singleton Object,SingleCall Object,Pool Object  )

連接池企業應用組件圖如下:

類圖如下:

主要文件代碼:

  /// <summary>

    /// 連接池上下文

    /// </summary>

    public sealed class ConnectionPoolContext : IDisposable

    {

        /// <summary>

        /// 池對象工廠

        /// </summary>

        private PoolObjectFactory _factory;

        /// <summary>

        /// 默認連接字符

        /// </summary>

        private string _defaultConnectionString;

        /// <summary>

        /// 工廠名稱

        /// </summary>

        private string _factoryName;

        /// <summary>

        /// 類型

        /// </summary>

        private Type _factoryType;

        /// <summary>

        /// SQL狀態倉庫

        /// </summary>

        private SqlStatementRepository _statementRepository;

        /// <summary>

        /// 連接池

        /// </summary>

        private static Dictionary<string, ConnectionPoolContext> _instances = 

new Dictionary<string, ConnectionPoolContext>();

        /// <summary>

        /// 默認連接字符屬性

        /// </summary>

        public string DefaultConnectionString

        {

            get

            {

                return _defaultConnectionString;

            }

            set

            {

                _defaultConnectionString = value;

            }

        }

        /// <summary>

        /// 工廠名

        /// </summary>

        public string FactoryName

        {

            get

            {

                return _factoryName;

            }

            set

            {

                _factoryName = value;

            }

        }

        /// <summary>

        /// SQL狀態倉庫屬性

        /// </summary>

        public SqlStatementRepository StatementRepository

        {

            get

            {

                if (_statementRepository == null)

                    _statementRepository = new SqlStatementRepository();

                return _statementRepository;

            }

        }

        /// <summary>

        /// 對象工廠模式

        /// </summary>

        internal PoolObjectFactory Factory

        {

            get

            {

                return _factory;

            }

            set

            {

                _factory = value;

            }

        }

        /// <summary>

        /// 實例化一個連接池

        /// </summary>

        /// <param name="id"></param>

        /// <returns></returns>

        public static ConnectionPoolContext Instance(string id)

        {

            return _instances[id];

        }

        /// <summary>

        /// 添加一個連接到池中

        /// </summary>

        /// <param name="id">id</param>

        /// <param name="factoryName">工廠名稱</param>

        /// <param name="connectionString">連接字符串</param>

        /// <param name="max">最大數</param>

        /// <param name="limit">是否限制</param>

        public static void AddPool(string id, string factoryName, string 

connectionString, int max, bool limit)

        {

            ConnectionPoolContext p = new ConnectionPoolContext();

            p._defaultConnectionString = connectionString;

            p._factoryName = factoryName;

            p.Factory = new PoolObjectFactory(max, limit, new ArrayList());

            _instances.Add(id, p);

        }

        /// <summary>

        /// 得到一個連接

        /// </summary>

        /// <returns>數據庫連接</returns>

        public DbConnection AcquireConnection()

        {

            if (_factoryType == null)

            {

                DbProviderFactory dbFactory = DbProviderFactories.GetFactory

(_factoryName);

                DbConnection conn = dbFactory.CreateConnection();

                _factoryType = conn.GetType();

            }

            DbConnection connection = (DbConnection)Factory.AcquireObject

(_factoryType);

            if (connection.ConnectionString == string.Empty)

                connection.ConnectionString = _defaultConnectionString;

            return connection;

        }

        /// <summary>

        /// 釋放一個連接

        /// </summary>

        /// <param name="conn">數據庫連接</param>

        public void ReleaseConnection(DbConnection conn)

        {

            Factory.ReleaseObject(conn);

            conn.Close();

        }

        #region IDisposable Members

        /// <summary>

        /// 釋放連接

        /// </summary>

        public void Dispose()

        {

            if (_factory != null)

                ((IDisposable)_factory).Dispose();

        }

        #endregion

        /// <summary>

        /// 私有構造函數

        /// </summary>

        private ConnectionPoolContext()

        {

        }

    }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved