/// <summary> /// 删除连接 /// </summary> protected void RemoveConnection() { Connections connections = 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); } } } } } }
/// <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); }
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); } } } }
/// <summary> /// 根据名称得到GraphWithoutTx的连接对象。如果不存在,自动创建一个新的连接对象(没有Open) /// </summary> /// <param name="connName">数据库连接名称</param> /// <returns>GraphWithoutTx的连接对象</returns> protected DbConnection GetConnectionWithoutTx(string connName) { ReferenceConnection refConnection = GetRefConnectionWithoutTx(connName); DbConnection connection = null; if (refConnection != null) { connection = refConnection.Connection; } return(connection); }
/// <summary> /// 释放连接 /// </summary> protected void ReleaseConnection() { Connections connections = GraphWithoutTx; lock (connections) { ReferenceConnection rConnection = null; if (connections.TryGetValue(this.Name, out rConnection)) { rConnection.ReferenceCount--; } } }
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); }
public override async Task <bool> Open(long auditKey, SelectQuery query, CancellationToken cancellationToken) { try { AuditKey = auditKey; if (_isOpen) { throw new ConnectionException("The reader is already open."); } _sqlConnection = await((ConnectionSql)ReferenceConnection).NewConnection(); _sqlReader = await ReferenceConnection.GetDatabaseReader(CacheTable, _sqlConnection, query, cancellationToken); _fieldCount = _sqlReader.FieldCount; _fieldOrdinals = new List <int>(); for (var i = 0; i < _sqlReader.FieldCount; i++) { var fieldName = _sqlReader.GetName(i); var ordinal = CacheTable.GetOrdinal(fieldName); if (ordinal < 0) { throw new ConnectionException($"The reader could not be opened as column {fieldName} could not be found in the table {CacheTable.Name}."); } _fieldOrdinals.Add(ordinal); } _sortFields = query?.Sorts; _isOpen = true; return(true); } catch (Exception ex) { throw new ConnectionException($"Open reader failed. {ex.Message}", ex); } }
/// <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); }