C# NHibernate 异常:事务未连接,或已断开连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15878493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NHibernate exception: Transaction not connected, or was disconnected
提问by Martijn
In our develop environment all the ASP.NET application works just fine. However, when I deploy the site on the test machine, on some pages I get this exception:
在我们的开发环境中,所有 ASP.NET 应用程序都运行良好。但是,当我在测试机器上部署站点时,在某些页面上出现此异常:
NHibernate.TransactionException: Transaction not connected, or was disconnected
at NHibernate.Transaction.AdoTransaction.CheckNotZombied() in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Transaction\AdoTransaction.cs:line 406
at NHibernate.Transaction.AdoTransaction.Rollback() in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Transaction\AdoTransaction.cs:line 240
I have no idea on how to solve this issue. The only difference are the DB versions: Develop: 10.0.5500 (2008 R2, SP1, Express) Test: 10.0.5500 (2008, SP3)
我不知道如何解决这个问题。唯一的区别是 DB 版本: 开发:10.0.5500(2008 R2,SP1,Express) 测试:10.0.5500(2008,SP3)
Does someone has an idea what is going on here?
有人知道这里发生了什么吗?
采纳答案by Jamie Ide
There's a problem with your session-per-request pattern implementation. ASP.NET is multi-threaded and the session is being closed when a thread terminates instead of when the request ends. There are many examplesof how to manage session-per-request and NHibernate has a built in NHibernate.Context.WebSessionContextbut I prefer to use a dependency injection framework such as Ninject.
您的 session-per-request 模式实现存在问题。ASP.NET 是多线程的,会话在线程终止而不是在请求结束时关闭。有很多关于如何管理 session-per-request 的例子,NHibernate 有一个内置的NHibernate.Context.WebSessionContext但我更喜欢使用依赖注入框架,比如Ninject。
回答by Daniel Little
This was a comment, but I had the same issue as well.
这是一个评论,但我也有同样的问题。
This error can occur when a trigger causes a database level exception. This will cause the transaction to be rolled back hence the exception.
当触发器导致数据库级别异常时,可能会发生此错误。这将导致事务回滚,因此出现异常。
回答by Alex M
Another problem could be the user security (a database level exception again). In my case the underlying DB problem was:
另一个问题可能是用户安全性(再次出现数据库级异常)。在我的情况下,底层的数据库问题是:
The server principal "AppUser" is not able to access the database "AppDB" under the current security context.