为什么临时表在 nhibernate 中不起作用?
我一直在尝试将临时表与 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下,在 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.