使用XMS访问IBM MQ时是否可以使用TransactionScope?我有这个测试代码:
var context =
new InitialContext(
new Hashtable
{
{ "XMSC_IC_URL", "XXX" },
{ "XMSC_IC_SECURITY_AUTHENTICATION", "none" },
});
var connectionFactory = (IConnectionFactory) context.Lookup("XXX");
connectionFactory.SetStringProperty("XMSC_WMQ_CONNECTION_MODE", "5");
connectionFactory.SetStringProperty("XMSC_WMQ_SSL_CIPHER_SPEC", "TRIPLE_DES_SHA_US");
connectionFactory.SetStringProperty("XMSC_WMQ_SSL_KEY_REPOSITORY", "XXX");
try
{
using (var scope = new TransactionScope())
{
using (var connection = connectionFactory.CreateConnection())
{
connection.Start();
using (var session = connection.CreateSession(true, AcknowledgeMode.SessionTransacted))
{
using (var queue = session.CreateQueue(queueName))
{
using (var consumer = session.CreateConsumer(queue))
{
var message = consumer.Receive();
}
}
}
}
throw new Exception();
scope.Complete();
}
}
catch (Exception)
{
}
尽管例外,它仍将提交消息的接收(确认)。为什么?如果我使用范围和行注释所有行并抛出异常,则消息将保留在队列中。为什么使用事务范围会在失败的情况下确认消息?