如何处理程序的exec异常? [英] How to deal with procedure exec exception?
问题描述
我正在以这种方式(通过使用NHibernate会话)调用带有param的过程:
I am calling procedure with param this way (by using NHibernate session):
var session = (NHibernate.ISession)GetConnection("BookDataBase");//NHibernate.ISession
var query = session.CreateSQLQuery("exec UpdateBook @BookId=:bookid");
query.SetInt32("bookid", bookId);
query.ExecuteUpdate();
除一件事情外,此方法工作正常.该部分在处理程序中被调用,因此在某些情况下该部分被连续两次调用(不同的更改但相同的bookId),并且有时它们可以同时运行.
This works fine except one thing. This part is called in handler, so there are situations when this part is called two times in a row (different changes but the same bookId), and ofcource, they can run simultaneously.
因此,当此部分尝试第二次运行时,我会收到此异常:
So, when this part are trying to run second time I am getting this exception:
事务未连接或已断开连接
Transaction not connected, or was disconnected
我想发生这种情况是因为当第二次更改尝试执行过程时,第一个已经执行了该过程并关闭了事务.
I suppose that this is happening because when second change tries to execute procedure, first one already did it and closed transaction.
如何防止这种情况?我需要能够同时执行2个过程.
How can I prevent this? I need to be able to execute 2 procedures simultaneously.
推荐答案
接着@ user1666620-您可能应该专注于此...
Following on from @user1666620 - you should probably focus around this...
每次请求会话模式存在问题 执行. ASP.NET是多线程的,会话正在 当线程终止而不是请求结束时关闭. 有许多示例说明如何管理每次请求会话和 NHibernate具有内置的NHibernate.Context.WebSessionContext,但我 宁愿使用诸如Ninject之类的依赖项注入框架.
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 examples of how to manage session-per-request and NHibernate has a built in NHibernate.Context.WebSessionContext but I prefer to use a dependency injection framework such as Ninject.
您需要确保在第一个查询完成后Nhibernate不会终止共享连接.
You need to make sure Nhibernate isn't killing a shared connection after the first query completes.
这篇关于如何处理程序的exec异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!