Пример #1
0
        /// <summary>
        /// Gets or creates a connection that can be used with the current session. If there is no active session, null is returned.
        /// This connection can and should be shared across threads for transaction rollbacks to work properly.
        /// </summary>
        public static IDbConnection GetConnection(string connectionString, Func<string, DbConnection> createConnection)
        {
            // Prevent rouge threads from access the shared connection
            Session.Tracker.AssertActivityStarted();

            string key;

            if (Transaction.TransactionInformation.DistributedIdentifier != Guid.Empty)
                key = Transaction.TransactionInformation.DistributedIdentifier.ToString();
            else
                key = Transaction.TransactionInformation.LocalIdentifier;

            key += "|" + connectionString;

            AmnesiaDbConnection connection = null;
            lock (connections)
            {
                if (!connections.TryGetValue(key, out connection))
                {
                    connection = new AmnesiaDbConnection(Transaction, createConnection(connectionString));
                    connections[key] = connection;
                }
            }

            return connection;
        }