Example #1
0
 /// <summary>
 /// Initializes a new instance of the ConnectionScope class with the specified option.
 /// </summary>
 /// <param name="options">The options to use.</param>
 /// <remarks>
 /// Initializes a new instance of the ConnectionScope class with the specified option and
 /// the default factory and connection string.
 /// If a TransactionScope is in effect, the connection will participate in that transaction.
 /// </remarks>
 public ConnectionScope(ConnectionScopeOptions options)
 {
     if (string.IsNullOrEmpty(DefaultSettings.ConnectionString))
     {
         throw new InvalidOperationException("ConnectionString property has not been initialized");
     }
     Initialize(DefaultSettings.Factory, DefaultSettings.ConnectionString, options);
 }
Example #2
0
        /// <summary>
        /// Initializer used only by constructors.
        /// </summary>
        private void Initialize(IDbFactory argFactory, string argConnectionString, ConnectionScopeOptions options)
        {
            Debug.Assert(!string.IsNullOrEmpty(argConnectionString));
            Debug.Assert(argFactory != null);

            this.options = options;

            if (stack == null)
            {
                stack = new Stack<Context>();
            }

            if (options == ConnectionScopeOptions.RequiresExisting && stack.Count == 0)
            {
                throw new InvalidOperationException("No connection exists for a ConnectionScope with option RequiresExisting");
            }

            if (options != ConnectionScopeOptions.RequiresNew && CanReuseExistingConnection(argFactory, argConnectionString))
            {
                // We can reuse an existing connection
                this.context = stack.Peek();
                this.context.ReferenceCount++;
                // Make sure that the connection is open
                AssertOpen(this.context.Connection);

                this.previousLockTimeoutMs = this.context.LockTimeoutMs;
                // Has a Transaction been started since the existing connection was created?
                if (this.context.Transaction == null && Transaction.Current != null)
                {
                    // Enlist the existing connection in the transaction.
                    // This must be done because the connection is older than the transaction.
                    ((DbConnection)this.context.Connection).EnlistTransaction(Transaction.Current);
                    this.context.Transaction = Transaction.Current;
                    // Instruct current transaction to notify us when the transaction completes
                    // (either sucessfully or unsuccessfully). This must be done so we don't enlist
                    // the next ConnectionScope in a transaction that has already been completed.
                    // Note that the event handler method is static.
                    Transaction.Current.TransactionCompleted += new TransactionCompletedEventHandler(CurrentTransactionCompleted);
                }
            }
            else
            {
                // We must open a new connection
                this.context = new Context(argFactory, argConnectionString);
                this.context.Connection.Open();
                SetLockTimeout(DefaultSettings.LockTimeoutMs);
                this.context.ReferenceCount++;
                this.previousLockTimeoutMs = DefaultSettings.LockTimeoutMs;
                if (Transaction.Current != null)
                {
                    this.context.Transaction = Transaction.Current;
                    Transaction.Current.TransactionCompleted += new TransactionCompletedEventHandler(CurrentTransactionCompleted);

                }
                stack.Push(context);
            }
        }