我有一段 vb.net 代码,它从 MQ 队列中读取消息并将其写入另一个队列。
它在事务(using New TransactionScope()
)中执行此操作,因此如果写入失败,读取将被回滚。
问题是,事务似乎仅在我关闭 .net 程序后提交。
使用 MQ 资源管理器,我看到消息移动,然后在队列状态中显示" Uncommitted Messages: Yes
"(并且消息保留在新队列中),直到 .net 程序关闭 - 此时消息由另一个程序 (IIB) 从队列中读取。
所以,在我看来,TransactionScope
并没有被处理掉;但我想不出为什么不这样做。
以下是相关代码:
WPF 在按下按钮时调用命令,该命令调用以下方法(简化):
' MQMessageDetails is just a data-storage class for message data
Friend Function MoveMessages(messages as IEnumerable(of MQMessageDetails))
For Each msg in messages
Try
Using ts As New TransactionScope()
success = WriteMessage(msg, "Q.OUT")
success = success and ReadMessage(msg)
If success Then
ts.Complete()
End If
End Using
Catch ....
End Try
Next
End Function
这是我在 MQ 资源管理器中看到的内容:
**Before program start**
Q.IN - depth 2, no uncommitted messages
Q.OUT - depth 0, no uncommitted messages
**Move button clicked**
Q.IN - depth 1, 1 uncommitted message
Q.OUT - depth 1, 1 uncommitted message
**Program closed**
Q.IN - depth 1, no uncommitted messages
Q.OUT - depth 0, no uncommitted messages <- the message was read by another program
从MQSeries的角度来看,.NET TransactionScope是一个外部协调的事务。MQ 只会在某些情况下参与外部协调的交易。
例如,您可能会发现在客户端环境中进行测试时需要 MQ 提交,但在生产服务器上不需要。
有关更多信息,请查看以下内容:
https://www.ibm.com/support/knowledgecenter/SSFKSJ_7.5.0/com.ibm.mq.pro.doc/q003570_.htm
和
http://www.mqseries.net/phpBB2/viewtopic.php?t=49148
IBM红皮书(无论过时如何)总是一本好书:
http://www.redbooks.ibm.com/redbooks/pdfs/sg247012.pdf