1

我们有一些看起来有点像这样的代码(错误处理和其他东西被删除)

using (var tran = conn.BeginTransaction())
{
    var client = new Service(...);
    var dialog = client.GetConversation(null, conn, tran);
    var response = dialog.Receive();

    // do stuff with response, including database work

    dialog.Send(message, conn, tran);
    dialog.EndConversation(conn, tran);

    tran.Commit();
    conn.Close();
}

我们继承了这段代码并且不是 ServiceBroker 的专家,如果我们像这样将对话移到事务之外会不会有问题:

var client = new Service(...);
var dialog = client.GetConversation(null, conn, tran);
var response = dialog.Receive();

using (var tran = conn.BeginTransaction())
{
    // do stuff with response, including database work
    tran.Commit();
}

dialog.Send(message, conn, tran);
dialog.EndConversation(conn, tran);
conn.Close();
4

1 回答 1

1

在这种情况下,您会收到消息并将其从队列中删除。您将无法再次收到它..

如果所有代码都在事务中并且消息处理中存在错误 - 事务永远不会提交并且消息留在队列中(默认情况下 - 在 5 次回滚后队列被禁用)。因此,您可以检测错误原因,纠正错误并再次处理消息(预期的异常不应导致回滚,有很多方法可以处理它们)。

我会说一切都应该在交易中。

于 2012-06-29T11:25:56.220 回答