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); }
internal static void InnerRemove(ConnectionWrapper item) { GetConnlockObj.EnterWriteLock(); try { ConnList.Remove(item); item.Dispose(); item = null; } finally { GetConnlockObj.ExitWriteLock(); } }
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(); } }