Пример #1
0
        public static MyDbConnection2 PopDBConnection(string dbKey)
        {
            MyDbConnection2 conn = null;

            DbHelperMySQL3.SemaphoreClientsNoPool.WaitOne();
            try
            {
                string connectionString;
                lock (DbHelperMySQL3.Mutex)
                {
                    if (!DbHelperMySQL3.ConnectionStringDict.TryGetValue(dbKey, out connectionString))
                    {
                        connectionString = PubConstant.ConnectionString;
                        string dbName       = PubConstant.GetDatabaseName(dbKey);
                        int    idx0         = connectionString.IndexOf("database=") + "database=".Length;
                        int    idx          = connectionString.IndexOf(';', idx0);
                        string datebaseName = connectionString.Substring(idx0, idx - idx0);
                        connectionString = connectionString.Replace(datebaseName, dbName);
                        DbHelperMySQL3.ConnectionStringDict[dbKey] = connectionString;
                    }
                }
                conn = new MyDbConnection2(connectionString, DbHelperMySQL3.CodePageNames);
                if (!conn.Open())
                {
                    conn = null;
                }
            }
            catch (Exception ex)
            {
                conn = null;
            }
            finally
            {
                if (null == conn)
                {
                    DbHelperMySQL3.SemaphoreClientsNoPool.Release();
                }
            }
            return(conn);
        }
Пример #2
0
        /// <summary>
        /// 从连接队列中取一个空闲数据库连接
        /// </summary>
        /// <returns></returns>
        public static MyDbConnection2 PopDBConnection(string dbKey)
        {
            MyDbConnection2 conn = null;

            if (UsePool)
            {
                int round = 0;
                MyDbConnectionPool pool = null;
                lock (Mutex)
                {
                    if (!DBConnsDict.TryGetValue(dbKey, out pool))
                    {
                        string connectionString = PubConstant.ConnectionString;
                        string dbName           = PubConstant.GetDatabaseName(dbKey);
                        if (null == dbName)
                        {
                            return(null);
                        }

                        int    idx0         = connectionString.IndexOf("database=") + "database=".Length;
                        int    idx1         = connectionString.IndexOf(';', idx0);
                        string datebaseName = connectionString.Substring(idx0, idx1 - idx0);

                        pool = new MyDbConnectionPool();
                        DBConnsDict[dbKey]    = pool;
                        pool.DatabaseKey      = dbKey;
                        pool.ConnectionString = connectionString.Replace(datebaseName, dbName);
                    }
                }

                if (null != pool)
                {
                    do
                    {
                        //防止无法获取, 阻塞等待
                        if (pool.SemaphoreClients.WaitOne(1000))
                        {
                            lock (pool.DBConns)
                            {
                                conn = pool.DBConns.Dequeue();
                                break;
                            }
                        }
                        else
                        {
                            lock (Mutex)
                            {
                                if (pool.ConnCount < MaxCount)
                                {
                                    try
                                    {
                                        conn = new MyDbConnection2(pool.ConnectionString, CodePageNames);
                                        if (conn.Open())
                                        {
                                            conn.DatabaseKey = dbKey;
                                            pool.ConnCount++;
                                            break;
                                        }
                                    }
                                    catch (System.Exception ex)
                                    {
                                        LogManager.WriteExceptionUseCache(ex.ToString());
                                    }
                                }
                            }
                        }
                    } while (round++ < 150);
                }
            }
            else
            {
                SemaphoreClientsNoPool.WaitOne();

                try
                {
                    string connectionString;
                    bool   change = false;
                    lock (Mutex)
                    {
                        if (!ConnectionStringDict.TryGetValue(dbKey, out connectionString))
                        {
                            connectionString = PubConstant.ConnectionString;
                            string dbName       = PubConstant.GetDatabaseName(dbKey);
                            int    idx0         = connectionString.IndexOf("database=") + "database=".Length;
                            int    idx1         = connectionString.IndexOf(';', idx0);
                            string datebaseName = connectionString.Substring(idx0, idx1 - idx0);
                            connectionString            = connectionString.Replace(datebaseName, dbName);
                            ConnectionStringDict[dbKey] = connectionString;
                        }
                    }

                    conn = new MyDbConnection2(connectionString, CodePageNames);
                    if (!conn.Open())
                    {
                        conn = null;
                    }
                }
                catch (System.Exception ex)
                {
                    conn = null;
                }
                finally
                {
                    if (null == conn)
                    {
                        SemaphoreClientsNoPool.Release();
                    }
                }
            }

            return(conn);
        }