我有一個需要同時包含MSSQL數據庫和IBM MQ隊列的.NET TransactionScope。如何使用.net TransactionScope從MQ執行事務GET?
我在完全託管模式下使用.NET 4.0(VS2010),SQL 2008R2,MQ Server 6.0,MQ Client 7.0.1.9。所有組件都在不同的機器上運行。
從我發現下面的模式應該工作: http://publib.boulder.ibm.com/infocenter/wmqv7/v7r1/index.jsp?topic=%2Fcom.ibm.mq.doc%2Fun11400_.htm
隱性交易 下面這段代碼描述了WebSphere MQ的.NET應用程序使用.NET隱性交易 規劃如何將消息。使用(TransactionScope scope = new TransactionScope()){Q.Put (putMsg,pmo);使用(TransactionScope scope = new TransactionScope()){Q.Post (putMsg,pmo); scope.Complete(); }
Q.close(); qMgr.Disconect();}
在我的代碼,這看起來像:
// mq properties
properties = new Hashtable();
properties.Add(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_MANAGED);
properties.Add(MQC.HOST_NAME_PROPERTY, HOSTNAME);
properties.Add(MQC.PORT_PROPERTY, PORTNUMBER);
properties.Add(MQC.CHANNEL_PROPERTY, CHANNELNAME);
_queueManager = new MQQueueManager(queueManagerName, properties);
_queue = _queueManager.AccessQueue(queueName, MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING);
_gmo = new MQGetMessageOptions();
_gmo.Options |= MQC.MQGMO_WAIT;
_gmo.Options |= MQC.MQGMO_SYNCPOINT;
_gmo.WaitInterval = 1000; // 1 seconds wait
// in a loop
using (TransactionScope t = new TransactionScope())
{
var message = new MQMessage();
try
{
_queue.Get(message, _gmo);
}
catch (MQException mqe)
{
message = null;
if (mqe.ReasonCode == 2033)
{
Console.WriteLine("No message available");
}
else
{
throw;
}
}
t.Complete();
}
//Afterwards:
if (_queue != null)
{
_queue.Close();
_queue = null;
}
if (_queueManager != null)
{
_queueManager.Disconnect();
_queueManager.Close();
_queueManager = null;
}
這樣做的問題是,所有的消息重新出現在隊列中的應用程序關閉後,而工作在SQL數據庫中提交。 如果事務處理器內發生異常,則SQL事務回滾,而MQ中的消息似乎保持被刪除(直到我重新啓動應用程序)。 此外,我沒有看到.NET客戶端和MQ服務器之間的任何DTC活動(是否預計?)
我有點迷失在這裏,任何幫助,非常感謝。
- 應該使用哪個MQ客戶端?託管,非託管,XA?
- 我應該看到DTC活動嗎?我將如何讓託管客戶參與?
- 應該在transactionscope中創建哪些對象?
- 爲什麼當應用程序結束時,即使提交transactionscope時,消息又重新出現在隊列中?
UPDATE
- 我有這個7.1客戶端和服務器上的工作,但要獲得(服務器)版本部署到我們的生產環境需要太長時間。
- 所以我需要在服務器端使用7.0.x版本,客戶端可以使用我需要的任何東西。
- 連接到使用7.1客戶端MQ6服務器失敗,錯誤代碼2354
您用於MQ連接的名稱空間是什麼? – Enthusiastic
IBM.WMQ。*,是MQ SDK的一部分;請參閱http://publib.boulder.ibm.com/infocenter/wmqv7/v7r1/topic/com.ibm.mq.doc/un10210_.htm – thijs
謝謝vmuch :) – Enthusiastic