Exemplo n.º 1
0
        public static IDbConnection GetConnection(string connectionString, IDbConnectionAdapter adapter)
        {
            if (adapter == null)
            {
                throw new ArgumentNullException(nameof(adapter));
            }
            Transaction current = Transaction.Current;

            if (current == null)
            {
                return(null);
            }
            Dictionary <string, DbConnectionWrapper> dictionary = null;

            lock (transactionConnections)
            {
                if (!transactionConnections.TryGetValue(current, out dictionary))
                {
                    dictionary = new Dictionary <string, DbConnectionWrapper>();
                    transactionConnections.Add(current, dictionary);
                    current.TransactionCompleted += OnTransactionCompleted;
                }
            }
            DbConnectionWrapper dbconnection = null;

            lock (dictionary)
            {
                if (!dictionary.TryGetValue(connectionString, out dbconnection))
                {
                    dictionary.Add(connectionString, dbconnection = new DbConnectionWrapper(adapter.Create(connectionString)));
                }
                return(dbconnection.GetConnection());
            }
        }
Exemplo n.º 2
0
        /// <summary> 获取数据库连接 </summary>
        /// <param name="connectionString">数据库连接</param>
        /// <param name="adapter">数据库适配器</param>
        /// <param name="threadCache">是否启用线程缓存(不启用缓存时优先读取事务连接池)</param>
        /// <returns></returns>
        public IDbConnection GetConnection(string connectionString, IDbConnectionAdapter adapter, bool threadCache = true)
        {
            lock (LockObj)
            {
                if (!threadCache)
                {
                    return(TransactionScopeConnections.GetConnection(connectionString, adapter) ?? adapter.Create(connectionString));
                }
                var connectionKey = Thread.CurrentThread;

                if (!ConnectionCache.TryGetValue(connectionKey, out Dictionary <string, DbConnectionWrapper> connDict))
                {
                    connDict = new Dictionary <string, DbConnectionWrapper>();
                    if (!ConnectionCache.TryAdd(connectionKey, connDict))
                    {
                        throw new Exceptions.DException($"{connectionString}数据库连接添加失败!");
                    }
                }
                if (connDict.TryGetValue(connectionString, out DbConnectionWrapper info))
                {
                    return(info.GetConnection());
                }
                info = new DbConnectionWrapper(adapter.Create(connectionString), TimeSpan.FromMinutes(Math.Min(60, Math.Max(5, adapter.ConnectionHeartbeat))));
                connDict.Add(connectionString, info);
                if (!_clearTimerRun)
                {
                    _clearTimer.Start();
                    _clearTimerRun = true;
                }
                return(info.GetConnection());
            }
        }