Example #1
0
        /// <summary>
        /// Creates a new instance of the DatabaseContext class.
        /// </summary>
        /// <param name="existingContext">The existing context.</param>
        /// <param name="databaseInfo">An object describing the database properties.</param>
        /// <param name="requireTransaction">A boolean indicating whether a transaction is required.</param>
        /// <param name="commandTimeout">The command timeout.</param>
        /// <param name="transactionTimeout">The transaction timeout.</param>
        /// <param name="enlistTransaction">if set to <c>true</c> [enlist transaction].</param>
        /// <param name="preventPostDisposeActionsPropagating">if set to <c>true</c> [prevent post dispose actions propagating].</param>
        /// <param name="createConnectionImmediately">if set to <c>true</c> [create connection immediately].</param>
        /// <param name="isUnitTestTransaction">if set to <c>true</c> [is unit test transaction].</param>
        /// <param name="newContext">if set to <c>true</c> [new context].</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">databaseInfo</exception>
        private static DatabaseContext CreateContext(DatabaseContext existingContext, DatabaseInfo databaseInfo, bool requireTransaction, int commandTimeout, int transactionTimeout, bool enlistTransaction, bool preventPostDisposeActionsPropagating, bool createConnectionImmediately, bool isUnitTestTransaction, out bool newContext)
        {
            if (databaseInfo == null)
            {
                throw new ArgumentNullException("databaseInfo");
            }

            newContext = false;

            bool saveTransaction = false;

            var context = new DatabaseContext
            {
                _databaseInfo       = databaseInfo,
                _commandTimeout     = commandTimeout,
                _transactionTimeout = transactionTimeout,
                _preventPostDisposeActionsPropagating = preventPostDisposeActionsPropagating
            };

            if (requireTransaction)
            {
                context._transactionOwner      = true;
                context._transactionActive     = true;
                context._isUnitTestTransaction = isUnitTestTransaction;


                if (existingContext != null && existingContext._transactionActive && enlistTransaction)
                {
                    context._transactionId = TransactionId.Create();
                    saveTransaction        = true;
                }
                else
                {
                    context._transactionScope = new Trans.TransactionScope(Trans.TransactionScopeOption.RequiresNew, new Trans.TransactionOptions
                    {
                        Timeout        = TimeSpan.FromSeconds(context._transactionTimeout),
                        IsolationLevel = Trans.IsolationLevel.ReadCommitted
                    });
                }

                if (existingContext == null || !existingContext._transactionActive || !databaseInfo.Equals(existingContext._databaseInfo))
                {
                    // A transaction is not active, create a new connection.
                    // This is required, because connections that are to participate in
                    // transactions must be created after the transaction scope is created.
                    context._connection      = new Lazy <IDbConnection>(() => CreateConnection(databaseInfo));
                    context._connectionOwner = true;

                    if (createConnectionImmediately)
                    {
                        var dummy = context._connection.Value;
                    }
                }
                else
                {
                    // A transaction is already active. Re-use the existing connection.
                    context._connection      = existingContext._connection;
                    context._connectionOwner = false;
                }

                /////
                // Save transaction once the context connection has been specified.
                /////
                if (saveTransaction)
                {
                    context.CreateCommand("SAVE TRANSACTION " + context._transactionId).ExecuteNonQuery();
                }

                newContext = true;
            }
            else
            {
                if (existingContext == null)
                {
                    context._connection      = new Lazy <IDbConnection>(() => CreateConnection(databaseInfo));
                    context._connectionOwner = true;

                    newContext = true;

                    if (createConnectionImmediately)
                    {
                        var dummy = context._connection.Value;
                    }
                }
                else
                {
                    context._connection      = existingContext._connection;
                    context._connectionOwner = false;

                    if (existingContext._transactionActive)
                    {
                        context._transactionActive = existingContext._transactionActive;
                        context._transactionOwner  = false;
                    }
                }
            }

            return(context);
        }