/// <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 rConnection = null; lock (connections) { if (connections.TryGetValue(this.Name, out rConnection) == false) { rConnection = new ReferenceConnection(this.Name, DbConnectionManager.GetConnection(this.Name)); IsConnectionCreator = true; connections.Add(this.Name, rConnection); } else { rConnection.ReferenceCount++; } } return(rConnection.Connection); }
/// <summary> /// 根据名称得到当前引用的连接 /// </summary> /// <param name="connName">数据库连接名称</param> /// <returns>连接对象</returns> protected ReferenceConnection GetRefConnectionWithoutTx(string connName) { ReferenceConnection refConnection = null; GraphWithoutTransaction connections = 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); }