我的高级代码执行以下操作:
这些步骤是在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