예제 #1
0
        public static IModelWrapper GetOrCreateChannel(ConnectionFactory connFactory)
        {
            if (connFactory == null)
            {
                throw new ArgumentNullException("参数connFactory不能为空,请检查参数");
            }

            GetConnlockObj.EnterUpgradeableReadLock();///若线程用光了,得写一个连接线程,所以这里用可升级锁
            IModelWrapper model = null;

            try
            {
                foreach (var item in ConnList)
                {
                    model = item.GetOrCreateChannel();
                    if (model != null)
                    {
                        return(model);
                    }
                }
                if (model == null)
                {
                    //创建connection对象
                    ConnectionWrapper conn = CreateConnection(connFactory);
                    return(conn.GetOrCreateChannel());
                }
            }
            finally
            {
                GetConnlockObj.ExitUpgradeableReadLock();
            }
            return(null);
        }
예제 #2
0
 internal static void InnerRemove(ConnectionWrapper item)
 {
     GetConnlockObj.EnterWriteLock();
     try
     {
         ConnList.Remove(item);
         item.Dispose();
         item = null;
     }
     finally
     {
         GetConnlockObj.ExitWriteLock();
     }
 }
예제 #3
0
 private static ConnectionWrapper CreateConnection(ConnectionFactory connFactory)
 {
     GetConnlockObj.EnterWriteLock();
     try
     {
         var newConn = connFactory.CreateConnection();
         ConnectionWrapper connWrapper = new ConnectionWrapper(connFactory, newConn);
         ConnList.Add(connWrapper);
         return(connWrapper);
     }
     finally
     {
         GetConnlockObj.ExitWriteLock();
     }
 }