Exemplo n.º 1
0
            /// <summary>
            /// Creates an ADO connection and enlists it in the specified transaction.
            /// </summary>
            /// <param name="transaction">
            /// The <see cref="Transaction" /> in which to enlist a new connection in.
            /// </param>
            /// <param name="shouldClose">
            /// Indicates whether the caller should close the connection or
            /// whether it is managed by an external entity.
            /// </param>
            /// <returns>
            /// A <see cref="DbConnection" /> that is enlisted in the specified
            /// <see cref="Transaction" />.
            /// </returns>
            public DbConnection CreateEnlistedConnection(Transaction transaction, out bool shouldClose)
            {
                if (transaction == null)
                {
                    throw new ArgumentNullException("transaction");
                }

                if (innerResourceProvider == null)
                {
                    throw new NotSupportedException(RM.Get_Error_ResourceProviderNotInitialised());
                }

                if (workBatchService.connectionsByTransaction.ContainsKey(transaction))
                {
                    DbConnection dbConnection;
                    Dictionary <String, DbConnection> connectionsByConnectionString = workBatchService.connectionsByTransaction[transaction];
                    if (!connectionsByConnectionString.TryGetValue(innerResourceProvider.ConnectionString, out dbConnection))
                    {
                        if (workBatchService.useLocalTransactions)
                        {
                            // we're using local transactions - create a new
                            // SinglePhaseOnlyTransactionAdapter to create a local
                            // transaction and prevent it from being promoted to
                            // a distributed transaction
                            try
                            {
                                dbConnection = innerResourceProvider.CreateConnection();
                                dbConnection.Open();

                                // can only do this once (additional enlistments will fail)
                                workBatchService.transactionAdapter = new SinglePhaseOnlyTransactionAdapter(dbConnection);
                                workBatchService.transactionAdapter.Begin();
                            }
                            catch
                            {
                                if (dbConnection != null)
                                {
                                    dbConnection.Dispose();
                                }

                                throw;
                            }
                        }
                        else
                        {
                            dbConnection = innerResourceProvider.CreateEnlistedConnection(transaction, out shouldClose);
                        }

                        connectionsByConnectionString.Add(innerResourceProvider.ConnectionString, dbConnection);
                    }

                    shouldClose = false;

                    return(dbConnection);
                }
                else
                {
                    throw new ArgumentNullException(RM.Get_Error_InvalidTransactionSpecified());
                }
            }