コード例 #1
0
        public static ConnectionWrapper GetConnection(string connectionString, DbProviderFactory dbProvider)
        {
            Transaction currentTran = Transaction.Current;

            if (currentTran == null)
            {
                return(new ConnectionWrapper(CreateConnection(connectionString, dbProvider)));
            }

            // tranConnections是一个多线程安全的字典对象,尽量必免使用锁带来的性能损耗。
            Dictionary <string, ConnectionWrapper> connWrappers =
                tranConnections.GetOrAdd(currentTran, x =>
            {
                x.TransactionCompleted += OnTransactionCompleted;
                return(new Dictionary <string, ConnectionWrapper>());
            });

            // connWrappers通常不会在并发环境下操作,也就不会发生锁竞争,对于锁带来的性能损耗可以忽略。此处上一把锁主要是为了安全起见。
            ConnectionWrapper connWrapper;

            lock (connWrappers)
            {
                if (!connWrappers.TryGetValue(connectionString, out connWrapper))
                {
                    connWrapper = new ConnectionWrapper(CreateConnection(connectionString, dbProvider));
                    connWrappers.Add(connectionString, connWrapper);
                }
                connWrapper.AddReference();
            }

            return(connWrapper);
        }
コード例 #2
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="connnection"></param>
 /// <param name="datareader"></param>
 public DataReaderWrapper(ConnectionWrapper connnection, IDataReader datareader)
 {
     this.connnection = connnection;
     this.datareader  = datareader;
     this.connnection.AddReference();
 }
コード例 #3
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="connectionString"></param>
 /// <returns></returns>
 public ConnectionWrapper GetWrapperedConnection(string connectionString)
 {
     return(ConnectionWrapper.GetConnection(connectionString, DbProviderFactory));
 }