C#TransactionScope有2个不同的数据库

时间:2015-11-21 13:23:08

标签: c# oracle transactions transactionscope devart

我正在研究Oracle数据库上的有害交易。

我有以下代码,见下文和2个问题。 targetConnection和sourceConnection都是Oracle数据库,位于不同的网络上。

1)当"完成"时,将首先提交哪个命令。方法叫做?

2)当其中一个连接在"完成"期间丢失时会发生什么?方法?例如:达到完整方法并提交targetCommand,但现在与sourceConnection的连接丢失。在我的情况下,应该回滚整个范围,因为无法提交sourceCommand。

private bool MergeRowNew(String mergeStmt, Dictionary<string, object> row, ColumnList columns)
      {
         bool result = false;
         bool targetTransaction = false;
         bool sourceTransaction = false;
         String mergeStmt2 = "merge into ...";
         Exception exception = null;

         using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew,
            new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted }))
         {

            try
            {
               //Using target connection
               using (OracleConnection targetConnection = new OracleConnection(this.target.ConnectionString))
               {
                  targetConnection.Open();
                  //Create new command for target
                  using (var targetCommand = targetConnection.CreateCommand(mergeStmt))
                  {
                     //Add parameters to target command
                     foreach (Column col in columns)
                     {
                        targetCommand.Parameters.Add(col.Name, row[col.Name]);
                     }
                     //Affected rows paramaeter
                     var rowCount = targetCommand.Parameters.Add(":numRows", OracleDbType.Integer, ParameterDirection.Output);
                     //execute insert in target db
                     targetCommand.ExecuteNonQuery();
                     //command succssesful when only one row was affected
                     if (Convert.ToInt32(rowCount.Value) == 1)
                     {
                        targetTransaction = true;
                     }
                  }

               }

               //If target transaction was succssesful then start the local transaction
               if (targetTransaction)
               {
                  //Using source connection
                  using (OracleConnection sourceConnection = new OracleConnection(this.source.ConnectionString))
                  {
                     sourceConnection.Open();
                     using (var sourceCommand = sourceConnection.CreateCommand(mergeStmt2))
                     {
                        sourceCommand.Parameters.Add("...","...");

                        var sourceRowcount = sourceCommand.Parameters.Add(":numRows", OracleDbType.Integer, ParameterDirection.Output);
                        sourceCommand.ExecuteNonQuery();
                        //command succssesful when only one row was affected
                        if (Convert.ToInt32(sourceRowcount.Value) == 1)
                        {
                           sourceTransaction = true;
                        }
                     }
                  }
               }
            }
            catch (Exception ex)
            {
               exception = ex;
            }
            //if both commands were succssesful then complete the scope
            if (targetTransaction && sourceTransaction)
            {
               scope.Complete();
               result = true;
            }
         } //End scope.

         //There was an exception. 
         if (exception != null)
         {
            //Do some logging.
         }

         return result;
      }

1 个答案:

答案 0 :(得分:0)

1)两个命令将同时提交

2)如果任何登记的连接失败,则应回滚整个范围