public static DbConnection GetConnection(Database db)
 {
     Dictionary<string, DbConnection> dictionary;
     DbConnection newOpenConnection;
     Transaction current = Transaction.Current;
     if (current == null)
     {
         return null;
     }
     transactionConnections.TryGetValue(current, out dictionary);
     if (dictionary != null)
     {
         dictionary.TryGetValue(db.ConnectionString.ToString(), out newOpenConnection);
         if (newOpenConnection != null)
         {
             return newOpenConnection;
         }
     }
     else
     {
         dictionary = new Dictionary<string, DbConnection>();
         lock (transactionConnections)
         {
             transactionConnections.Add(current, dictionary);
         }
     }
     if (dictionary.ContainsKey(db.ConnectionString))
     {
         return dictionary[db.ConnectionString];
     }
     newOpenConnection = db.GetNewOpenConnection();
     current.TransactionCompleted += new TransactionCompletedEventHandler(TransactionScopeConnections.OnTransactionCompleted);
     dictionary.Add(db.ConnectionString, newOpenConnection);
     return newOpenConnection;
 }
        public static DbConnection GetConnection(Database db)
        {
            Transaction currentTransaction = Transaction.Current;

            if (currentTransaction == null)
            {
                return(null);
            }
            Dictionary <string, DbConnection> connectionList;
            DbConnection connection;

            lock (transactionConnections)
            {
                if (!transactionConnections.TryGetValue(currentTransaction, out connectionList))
                {
                    connectionList = new Dictionary <string, DbConnection>();
                    transactionConnections.Add(currentTransaction, connectionList);
                    currentTransaction.TransactionCompleted += OnTransactionCompleted;
                }
            }
            lock (connectionList)
            {
                if (!connectionList.TryGetValue(db.ConnectionString, out connection))
                {
                    connection = db.GetNewOpenConnection();
                    connectionList.Add(db.ConnectionString, connection);
                }
            }
            return(connection);
        }
Example #3
0
        public static DatabaseConnectionWrapper GetConnection(Database db)
        {
            Transaction current = Transaction.Current;

            if (current == (Transaction)null)
            {
                return((DatabaseConnectionWrapper)null);
            }
            Dictionary <string, DatabaseConnectionWrapper> dictionary;

            lock (TransactionScopeConnections.transactionConnections)
            {
                if (!TransactionScopeConnections.transactionConnections.TryGetValue(current, out dictionary))
                {
                    dictionary = new Dictionary <string, DatabaseConnectionWrapper>();
                    TransactionScopeConnections.transactionConnections.Add(current, dictionary);
                    current.TransactionCompleted += new TransactionCompletedEventHandler(TransactionScopeConnections.OnTransactionCompleted);
                }
            }
            DatabaseConnectionWrapper connectionWrapper;

            lock (dictionary)
            {
                if (!dictionary.TryGetValue(db.ConnectionString, out connectionWrapper))
                {
                    connectionWrapper = new DatabaseConnectionWrapper(db.GetNewOpenConnection());
                    dictionary.Add(db.ConnectionString, connectionWrapper);
                }
                connectionWrapper.AddRef();
            }
            return(connectionWrapper);
        }
Example #4
0
        /// <summary>
        ///		Returns a connection for the current transaction. This will be an existing <see cref="DbConnection"/>
        ///		instance or a new one if there is a <see cref="TransactionScope"/> active. Otherwise this method
        ///		returns null.
        /// </summary>
        /// <param name="db"></param>
        /// <returns>Either a <see cref="DbConnection"/> instance or null.</returns>
        public static DbConnection GetConnection(Database db)
        {
            Transaction currentTransaction = Transaction.Current;

            if (currentTransaction == null)
            {
                return(null);
            }

            Dictionary <string, DbConnection> connectionList;

            transactionConnections.TryGetValue(currentTransaction, out connectionList);

            DbConnection connection;

            if (connectionList != null)
            {
                connectionList.TryGetValue(db.ConnectionString.ToString(), out connection);
                if (connection != null)
                {
                    return(connection);
                }
            }
            else
            {                                                                                   // We don't have a list for this transaction, so create a new one
                connectionList = new Dictionary <string, DbConnection>();
                lock (transactionConnections)
                    transactionConnections.Add(currentTransaction, connectionList);
            }

            //
            // Next we'll see if there is already a connection. If not, we'll create a new connection and add it
            // to the transaction's list of connections.
            //
            if (connectionList.ContainsKey(db.ConnectionString))
            {
                connection = connectionList[db.ConnectionString];
            }
            else
            {
                connection = db.GetNewOpenConnection();
                currentTransaction.TransactionCompleted += new TransactionCompletedEventHandler(OnTransactionCompleted);
                connectionList.Add(db.ConnectionString, connection);
            }

            return(connection);
        }
        /// <summary>
        ///        Returns a connection for the current transaction. This will be an existing <see cref="DbConnection"/>
        ///        instance or a new one if there is a <see cref="TransactionScope"/> active. Otherwise this method
        ///        returns null.
        /// </summary>
        /// <param name="db"></param>
        /// <returns>Either a <see cref="DbConnection"/> instance or null.</returns>
        public static DatabaseConnectionWrapper GetConnection(Database db)
        {
            Transaction currentTransaction = Transaction.Current;

            if (currentTransaction == null)
            {
                return(null);
            }

            Dictionary <string, DatabaseConnectionWrapper> connectionList;
            DatabaseConnectionWrapper connection;

            lock (transactionConnections)
            {
                if (!transactionConnections.TryGetValue(currentTransaction, out connectionList))
                {
                    // We don't have a list for this transaction, so create a new one
                    connectionList = new Dictionary <string, DatabaseConnectionWrapper>();
                    transactionConnections.Add(currentTransaction, connectionList);

                    // We need to know when this previously unknown transaction is completed too
                    currentTransaction.TransactionCompleted += OnTransactionCompleted;
                }
            }

            lock (connectionList)
            {
                // Next we'll see if there is already a connection. If not, we'll create a new connection and add it
                // to the transaction's list of connections.
                // This collection should only be modified by the thread where the transaction scope was created
                // while the transaction scope is active.
                // However there's no documentation to confirm this, so we err on the safe side and lock.
                if (!connectionList.TryGetValue(db.ConnectionString, out connection))
                {
                    // we're betting the cost of acquiring a new finer-grained lock is less than
                    // that of opening a new connection, and besides this allows threads to work in parallel
                    var dbConnection = db.GetNewOpenConnection();
                    connection = new DatabaseConnectionWrapper(dbConnection);
                    connectionList.Add(db.ConnectionString, connection);
                }
                connection.AddRef();
            }

            return(connection);
        }
        /// <summary>
        ///		Returns a connection for the current transaction. This will be an existing <see cref="DbConnection"/>
        ///		instance or a new one if there is a <see cref="TransactionScope"/> active. Otherwise this method
        ///		returns null.
        /// </summary>
        /// <param name="db"></param>
        /// <returns>Either a <see cref="DbConnection"/> instance or null.</returns>
        public static DatabaseConnectionWrapper GetConnection(Database db)
        {
            Transaction currentTransaction = Transaction.Current;

            if (currentTransaction == null)
                return null;

            Dictionary<string, DatabaseConnectionWrapper> connectionList;
            DatabaseConnectionWrapper connection;

            lock (transactionConnections)
            {
                if (!transactionConnections.TryGetValue(currentTransaction, out connectionList))
                {
                    // We don't have a list for this transaction, so create a new one
                    connectionList = new Dictionary<string, DatabaseConnectionWrapper>();
                    transactionConnections.Add(currentTransaction, connectionList);

                    // We need to know when this previously unknown transaction is completed too
                    currentTransaction.TransactionCompleted += OnTransactionCompleted;
                }
            }

            lock (connectionList)
            {
                // Next we'll see if there is already a connection. If not, we'll create a new connection and add it
                // to the transaction's list of connections.
                // This collection should only be modified by the thread where the transaction scope was created
                // while the transaction scope is active.
                // However there's no documentation to confirm this, so we err on the safe side and lock.
                if (!connectionList.TryGetValue(db.ConnectionString, out connection))
                {
                    // we're betting the cost of acquiring a new finer-grained lock is less than 
                    // that of opening a new connection, and besides this allows threads to work in parallel
                    var dbConnection = db.GetNewOpenConnection();
                    connection = new DatabaseConnectionWrapper(dbConnection);
                    connectionList.Add(db.ConnectionString, connection);
                }
                connection.AddRef();
            }

            return connection;
        }
		/// <summary>
		///		Returns a connection for the current transaction. This will be an existing <see cref="DbConnection"/>
		///		instance or a new one if there is a <see cref="TransactionScope"/> active. Otherwise this method
		///		returns null.
		/// </summary>
		/// <param name="db"></param>
		/// <returns>Either a <see cref="DbConnection"/> instance or null.</returns>
		public static DbConnection GetConnection(Database db)
		{
			Transaction currentTransaction = Transaction.Current;

			if (currentTransaction == null)
				return null;

			Dictionary<string, DbConnection> connectionList;
			transactionConnections.TryGetValue(currentTransaction, out connectionList);

			DbConnection connection;
			if (connectionList != null)
			{
				connectionList.TryGetValue(db.ConnectionString.ToString(), out connection);
				if (connection != null)
					return connection;
			}
			else
			{									// We don't have a list for this transaction, so create a new one
				connectionList = new Dictionary<string, DbConnection>();
				lock (transactionConnections)
					transactionConnections.Add(currentTransaction, connectionList);
			}

			//
			// Next we'll see if there is already a connection. If not, we'll create a new connection and add it
			// to the transaction's list of connections.
			//
			if (connectionList.ContainsKey(db.ConnectionString))
				connection = connectionList[db.ConnectionString];
			else
			{
				connection = db.GetNewOpenConnection();
				currentTransaction.TransactionCompleted += new TransactionCompletedEventHandler(OnTransactionCompleted);
				connectionList.Add(db.ConnectionString, connection);
			}

			return connection;
		}