/// <summary> /// 从分布式事务环境中获取数据库链接对象。 /// </summary> /// <param name="database">数据库对象。</param> /// <returns>如果未启用分布式事务,则为 null,否则为对应 <see cref="IDatabase"/> 的数据库链接对象。</returns> public static DbConnection GetConnection(IDatabase database) { var curTrans = Transaction.Current; if (curTrans == null) { return(null); } if (!_transConns.TryGetValue(curTrans, out SafetyDictionary <string, DbConnection> connDictionary)) { connDictionary = new SafetyDictionary <string, DbConnection>(); _transConns.TryAdd(curTrans, connDictionary); Tracer.Debug($"Transaction registered."); curTrans.TransactionCompleted += OnTransactionCompleted; } var connStr = database.ConnectionString.ToString(); if (!connDictionary.TryGetValue(connStr, out DbConnection connection)) { connection = database.Provider.DbProviderFactory.CreateConnection(); if (connection != null) { connection.ConnectionString = connStr; connection.TryOpen(); connection.EnlistTransaction(curTrans); connDictionary.TryAdd(connStr, connection); Tracer.Debug($"DbConnection of '{connStr}' registered."); } } else { Tracer.Debug($"DbConnection get from cache."); } return(connection); }