/// <summary>
        /// 重写获取当前事物所使用的数据连接的函数
        /// </summary>
        /// <param name="ts">事物对象</param>
        /// <returns>数据连接</returns>
        protected override DbConnection OnGetConnectionWithTransaction(Transaction ts)
        {
            Connections connections = null;

            GraphWithTransaction graph = AutoEnlistDbContext.GraphWithTx;

            lock (graph)
            {
                // current transaction exists only in current HttpContext or Thread
                if (graph.TryGetValue(ts, out connections) == false)
                {
                    connections = new Connections();
                    graph.Add(Transaction.Current, connections);
                }
            }

            ReferenceConnection resultConnection = null;

            DoSafeConnectionOp(this.Name, connections,
                               (refConnection) =>
            {
                refConnection.ReferenceCount++;
                resultConnection = refConnection;
            },
                               () =>
            {
                resultConnection    = new ReferenceConnection(this.Name, DbConnectionManager.GetConnection(this.Name));
                IsConnectionCreator = true;

                connections.Add(this.Name, resultConnection);
            });

            return(resultConnection.Connection);
        }
Example #2
0
        /// <summary>
        /// 删除连接
        /// </summary>
        protected void RemoveConnection()
        {
            Connections connections = this.GraphWithoutTx;

            lock (connections)
            {
                ReferenceConnection rConnection = null;

                if (connections.TryGetValue(this.Name, out rConnection))
                {
                    if (rConnection.ReferenceCount == 0)
                    {
                        if (this.IsInTransaction == false)
                        {
                            try
                            {
                                if (rConnection.Connection.State != ConnectionState.Closed)
                                {
                                    rConnection.Connection.Close();
                                }

                                WriteTraceInfo(rConnection.Connection.DataSource + "." + rConnection.Connection.Database
                                               + "[" + DateTime.Now.ToString("yyyyMMdd HH:mm:ss.fff") + "]",
                                               " Close Connection ");
                            }
                            finally
                            {
                                connections.Remove(this.Name);
                            }
                        }
                    }
                }
            }
        }
Example #3
0
        protected override void OnTransactionCompleted(TransactionEventArgs args)
        {
            GraphWithTransaction graph = GraphWithTx;

            lock (graph)
            {
                DbTransactions transactions;

                if (graph.TryGetValue(args.Transaction, out transactions))
                {
                    try
                    {
                        lock (transactions)
                        {
                            foreach (KeyValuePair <ReferenceConnection, DbTransaction> item in transactions)
                            {
                                if (args.Transaction.TransactionInformation.Status == TransactionStatus.Committed)
                                {
                                    item.Value.Commit();
                                }
                                else
                                {
                                    item.Value.Rollback();
                                }

                                ReferenceConnection refConnection = item.Key;

                                if (refConnection.ReferenceCount == 0)
                                {
                                    try
                                    {
                                        refConnection.Connection.Close();
                                    }
                                    finally
                                    {
                                        GraphWithoutTx.Remove(refConnection.Name);
                                    }
                                }

                                WriteTraceInfo(refConnection.Connection.DataSource + "." + refConnection.Connection.Database
                                               + "[" + DateTime.Now.ToString("yyyyMMdd HH:mm:ss.fff") + "]",
                                               " Close Connection ");
                            }
                        }
                    }
                    finally
                    {
                        graph.Remove(args.Transaction);
                    }
                }
            }
        }
Example #4
0
        /// <summary>
        /// 根据名称得到GraphWithoutTx的连接对象。如果不存在,自动创建一个新的连接对象(没有Open)
        /// </summary>
        /// <param name="connName">数据库连接名称</param>
        /// <returns>GraphWithoutTx的连接对象</returns>
        protected DbConnection GetConnectionWithoutTx(string connName)
        {
            ReferenceConnection refConnection = this.GetRefConnectionWithoutTx(connName);

            DbConnection connection = null;

            if (refConnection != null)
            {
                connection = refConnection.Connection;
            }

            return(connection);
        }
Example #5
0
        /// <summary>
        /// 释放连接
        /// </summary>
        protected void ReleaseConnection()
        {
            Connections connections = this.GraphWithoutTx;

            lock (connections)
            {
                ReferenceConnection rConnection = null;

                if (connections.TryGetValue(this.Name, out rConnection))
                {
                    rConnection.ReferenceCount--;
                }
            }
        }
Example #6
0
        /// <summary>
        /// 根据名称得到当前引用的连接
        /// </summary>
        /// <param name="connName">数据库连接名称</param>
        /// <returns>连接对象</returns>
        protected ReferenceConnection GetRefConnectionWithoutTx(string connName)
        {
            ReferenceConnection result = null;

            DoSafeConnectionOp(connName,
                               this.GraphWithoutTx,
                               (refConnection) =>
            {
                refConnection.ReferenceCount++;
                result = refConnection;
            },
                               () =>
            {
                DbConnection connection   = DbConnectionManager.GetConnection(connName);
                this._isConnectionCreator = true;

                result = new ReferenceConnection(connName, connection);
                this.GraphWithoutTx.Add(connName, result);
            });

            return(result);
        }
Example #7
0
        /// <summary>
        /// 执行线程安全的连接操作
        /// </summary>
        /// <param name="connName"></param>
        /// <param name="connections"></param>
        /// <param name="foundAction"></param>
        /// <param name="notFoundAction"></param>
        protected void DoSafeConnectionOp(string connName, Connections connections, Action <ReferenceConnection> foundAction, Action notFoundAction = null)
        {
            lock (connections)
            {
                ReferenceConnection refConnection = null;

                if (connections.TryGetValue(connName, out refConnection) == false)
                {
                    if (notFoundAction != null)
                    {
                        notFoundAction();
                    }
                }
                else
                {
                    if (foundAction != null)
                    {
                        foundAction(refConnection);
                    }
                }
            }
        }
Example #8
0
        protected override DbConnection OnGetConnectionWithTransaction(Transaction ts)
        {
            ReferenceConnection refConnection = GetRefConnectionWithoutTx(this.Name);

            DbConnection connection = refConnection.Connection;

            if (connection.State == ConnectionState.Closed)
            {
                connection.Open();
            }

            DbTransactions localTxs = null;

            lock (GraphWithTx)
            {
                if (GraphWithTx.TryGetValue(ts, out localTxs) == false)
                {
                    localTxs = new DbTransactions();
                    GraphWithTx.Add(ts, localTxs);
                }
            }

            DbTransaction localTx = null;

            lock (localTxs)
            {
                if (localTxs.TryGetValue(refConnection, out localTx) == false)
                {
                    localTx = connection.BeginTransaction();

                    localTxs.Add(refConnection, localTx);
                }
            }

            this.LocalTransaction = localTx;

            return(connection);
        }
Example #9
0
        /// <summary>
        /// 根据名称得到当前引用的连接
        /// </summary>
        /// <param name="connName">数据库连接名称</param>
        /// <returns>连接对象</returns>
        protected ReferenceConnection GetRefConnectionWithoutTx(string connName)
        {
            ReferenceConnection refConnection = null;

            GraphWithoutTransaction connections = this.GraphWithoutTx;

            lock (connections)
            {
                if (connections.TryGetValue(connName, out refConnection) == false)
                {
                    DbConnection connection = DbConnectionManager.GetConnection(connName);
                    this._isConnectionCreator = true;

                    refConnection = new ReferenceConnection(connName, connection);
                    connections.Add(connName, refConnection);
                }
                else
                {
                    refConnection.ReferenceCount++;
                }
            }

            return(refConnection);
        }