在事务中不按顺序执行 SP

发布于 2024-11-14 05:39:58 字数 890 浏览 2 评论 0原文

使用SQL Server 2005。

每次接收到新数据时,都会将其存储起来,然后调用 SP,使用该数据来计算一些统计数据。存储数据和调用 SP 在单个事务中执行。 SP 被登记在事务中,就像

using (var transaction = session.BeginTransaction())
{
    // ... executing insert/update statements ...

    var command = new SqlCommand("StoredProcedureName", connection);

    command.CommandType = CommandType.StoredProcedure;
    command.Parameters.Add(new SqlParameter("@name", value));

    transaction.Enlist(command);
    command.ExecuteNonQuery();

    transaction.Commit();
}

运行 SQL Profiler 时一样,我可以看到所有插入/更新语句仅在调用 transaction.Commit(); 方法后才显示在那里,但 SP 调用在调用后立即显示command.ExecuteNonQuery(); 方法。因此,SP 调用显示在 SQL Profiler 中的插入/更新语句之前。这让我认为 SP 实际上是在任何插入/更新语句之前执行的,这在逻辑上是错误的。

问题是为什么在调用command.ExecuteNonQuery();方法后,SQL Profiler中会显示SP调用,而不是transaction.Commit();呢?这是否意味着它是立即执行的,而不是在执行插入/更新语句之后执行?

SQL Server 2005 is used.

Each time a new data is received it is stored and a SP is called after that which uses this data to calculate some statistics data. Storing data and calling the SP is executed in a single transaction. The SP is enlisted in the transaction like

using (var transaction = session.BeginTransaction())
{
    // ... executing insert/update statements ...

    var command = new SqlCommand("StoredProcedureName", connection);

    command.CommandType = CommandType.StoredProcedure;
    command.Parameters.Add(new SqlParameter("@name", value));

    transaction.Enlist(command);
    command.ExecuteNonQuery();

    transaction.Commit();
}

When running SQL Profiler I can see that all insert/update statements are shown there only after calling transaction.Commit(); method but the SP call is shown immediately after calling command.ExecuteNonQuery(); method. As a result the SP call is shown before insert/update statements in SQL Profiler. It makes me think that the SP is actually executed before any insert/update statements which is logically wrong.

The question is why the SP call is shown in SQL Profiler after calling command.ExecuteNonQuery(); method and not transaction.Commit(); one? Does it mean that it is executed immediately and not after executing insert/update statements?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷默言语 2024-11-21 05:39:58

NHibernate 会话的工作方式类似于工作单元。所有更改都存储在会话中,并且仅当您提交事务时它们才会保留在一起,因此在提交它们之前您无法使用过程,因为它们尚未存储在数据库中。

您必须在内存中进行计算(如果可能)或在提交事务后运行存储过程。

NHibernate session works like unit of work. All changes are stored in the session and they are persisted together only when you commit the transaction so you cannot use your procedure before you commit them because they haven't been stored in the database yet.

You must either do your computation in memory (if possible) or run your stored procedure after you commit transaction.

一生独一 2024-11-21 05:39:58

SQL Profiler 在语句执行时显示它们。使用 ExecuteReader 和 ExecuteNonReader 运行的语句是同步的,因此插入/更新语句将显示在 SQL Profiler 中的过程之前。

因此,寻找解释,您是如何执行插入/更新语句的?您是否有一个数据库层要等到commit才开始向服务器发送 SQL?

SQL Profiler shows statements as they are executed. Statements run with ExecuteReader and ExecuteNonReader are synchronous, so the insert/update statements would show before the procedure in SQL Profiler.

So looking for an explanation, how are you doing the insert/update statements? Is it possible that you have a database layer that waits until commit before it starts sending out SQL to the server?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文