Exemple #1
0
        /// <summary>
        /// 将数据库连接添加到空闲数据库连接中
        /// </summary>
        /// <param name="conn"></param>
        public static void PushDBConnection(MyDbConnection2 conn)
        {
            if (UsePool)
            {
                if (null != conn)
                {
                    MyDbConnectionPool pool = null;
                    lock (Mutex)
                    {
                        if (!DBConnsDict.TryGetValue(conn.DatabaseKey, out pool))
                        {
                            pool = new MyDbConnectionPool();
                            DBConnsDict[conn.DatabaseKey] = pool;
                        }
                    }

                    if (null != pool)
                    {
                        lock (Mutex)
                        {
                            if (pool.ConnCount <= MaxCount && conn.IsConnected())
                            {
                                lock (pool.DBConns)
                                {
                                    pool.DBConns.Enqueue(conn);
                                }

                                pool.SemaphoreClients.Release();
                            }
                            else
                            {
                                pool.ConnCount--;
                                conn.Close();
                            }
                        }
                    }
                }
            }
            else
            {
                if (conn != null)
                {
                    SemaphoreClientsNoPool.Release();
                    conn.Close();
                }
            }
        }