IBM MQQueueManager不会在TransactionScope C#下回滚

时间:2019-07-10 10:28:01

标签: c# transactionscope

我的高级代码执行以下操作:

  1. 从SQL表获取消息。
  2. 将其放在IBM队列中。
  3. 已发送更新SQL表消息。

这些步骤是在C#TransactionScope中完成的。

当作为测试用例之一从代码引发异常(尝试明确地执行此异常)时,TransactionScope会回滚SQL更新,但不会回滚IBM队列上的消息。

在Web服务器上启用了DTC。

#region Send Message                            
                        for (int n = 0; n < rowIds.Count; n++)
                        {
                            using (TransactionScope scope = new TransactionScope())
                            {
                                try
                                {
                                    Transaction.Current.TransactionCompleted += Current_TransactionCompleted;
                                    using (var wmq = new WmqConnection())
                                    {

                                        byte[] msgId = wmq.WriteMsg(rowsData[n]);

                                        //writes to the text file
                                        writer.WriteLine("MessageID:" + BitConverter.ToString(msgId) + " ||RaInterfaceDataID:" + rowIds[n] + " ||Message:" + rowsData[n]);

                                        using (SqlConnection connection2 = ConnectionFactory.GetConnection())
                                        {
                                            #region Mark Interface Data Complete
                                            connection2.Open();
                                            sqlCommand = new SqlCommand("Update InterfaceData Set Status = 'C' Where IntrfceDtaID = " + rowIds[n] + " and Status = 'F'", connection2);
                                            rowsEffected = sqlCommand.ExecuteNonQuery();
                                            if (rowsEffected != 1)
                                                throw new TransactionAbortedException();
                                            #endregion
                                        }

                                        throw new TransactionAbortedException();

                                    }

                                    //scope.Complete();
                                }
                                catch (TransactionAbortedException tex)
                                {
                                    throw tex;
                                }

                            } //transaction scope ends

                            n++;
                        } //send message ends

//发送消息的代码(另一种方法)

ConnectMQ();
                MQConnectOptions mco = new MQConnectOptions();
                queue = queueManager.AccessQueue(queueName, MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING);
                queueMessage = new MQMessage();
                queueMessage.WriteString(inputMsg);
                queueMessage.Format = MQC.MQFMT_STRING;
                queuePutMessageOptions = new MQPutMessageOptions();
                queuePutMessageOptions.Options = MQC.MQPMO_SYNCPOINT; 
                queue.Put(queueMessage, queuePutMessageOptions);
                return queueMessage.MessageId

0 个答案:

没有答案