为什么临时表在 nhibernate 中不起作用?

发布于 2025-01-07 14:13:11 字数 1147 浏览 4 评论 0原文

我一直在尝试将临时表与 nhibernate 一起使用。

以下代码不起作用

query = @"CREATE TABLE [#Dataset_x]
          ([Name] [nvarchar](max) NULL,
           [Value] [nvarchar](max) NULL )";
Session.CreateSQLQuery(query).SetTimeout(uploadExecQueryTimeout).ExecuteUpdate();

query = @"INSERT INTO [#Dataset_x] ([Name],[Value]) VALUES('Dataset','MyDataset')";
Session.CreateSQLQuery(query).SetTimeout(uploadExecQueryTimeout).ExecuteUpdate();

当我尝试运行第二个查询时,我收到对象无效错误(因为在我调用第二个查询之前创建的临时表已过期)。

但是,如果我将上面的代码添加到事务下,如下所示,它就可以正常工作。

using (var transaction = Session.BeginTransaction())
{
    query = @"CREATE TABLE [#Dataset_x]
              ([Name] [nvarchar](max) NULL,
               [Value] [nvarchar](max) NULL )";

    Session.CreateSQLQuery(query).SetTimeout(uploadExecQueryTimeout).ExecuteUpdate();

    query = @"INSERT INTO [#Dataset_x] ([Name],[Value]) VALUES('Dataset','MyDataset')";
    Session.CreateSQLQuery(query).SetTimeout(uploadExecQueryTimeout).ExecuteUpdate();
    transaction.Commit();
}

谁能告诉我为什么事务需要在 nhibernate 中使用临时表?

注意:我在配置中使用的“current_session_context_class”值是 call

I have been trying to use temporary tables along with nhibernate.

The following piece of code doesn't work

query = @"CREATE TABLE [#Dataset_x]
          ([Name] [nvarchar](max) NULL,
           [Value] [nvarchar](max) NULL )";
Session.CreateSQLQuery(query).SetTimeout(uploadExecQueryTimeout).ExecuteUpdate();

query = @"INSERT INTO [#Dataset_x] ([Name],[Value]) VALUES('Dataset','MyDataset')";
Session.CreateSQLQuery(query).SetTimeout(uploadExecQueryTimeout).ExecuteUpdate();

And I get the object invalid error when I try to run the second query (because the temporary table that was created expired before I called in the second query).

However if I add the above piece of code under a transaction as shown below it works perfectly fine.

using (var transaction = Session.BeginTransaction())
{
    query = @"CREATE TABLE [#Dataset_x]
              ([Name] [nvarchar](max) NULL,
               [Value] [nvarchar](max) NULL )";

    Session.CreateSQLQuery(query).SetTimeout(uploadExecQueryTimeout).ExecuteUpdate();

    query = @"INSERT INTO [#Dataset_x] ([Name],[Value]) VALUES('Dataset','MyDataset')";
    Session.CreateSQLQuery(query).SetTimeout(uploadExecQueryTimeout).ExecuteUpdate();
    transaction.Commit();
}

Can anyone suggest me why transactions are required to use temporary tables in nhibernate?

Note: The "current_session_context_class" value which I used in my config is call

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

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

发布评论

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

评论(1

幻想少年梦 2025-01-14 14:13:11

默认情况下,在 NHibernate 中,每个事务都会打开和关闭数据库连接。在第一个代码片段中,每个语句都在单独的隐式事务中执行,因此 NHibernate 在每个语句之后关闭连接。在第二个代码片段中,仅当您结束事务时连接才会关闭,因此您的临时表在事务结束之前不会被删除。顺便说一句,NH 中隐式事务的使用是
不鼓励

By default in NHibernate a database connection is opened and closed for each transaction. In the first code snippet each statement is executed in a separate implicit transaction so NHibernate closes connection after each statement. In the second code snippet connection is closed only when you end transaction so your temporary table is not dropped till the end of transaction. By the way use of implicit transactions in NH is
discouraged.

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