使用ThreadPool时如何关闭NHibenrate会话?
由于线程被 ThreadPool 一遍又一遍地重用,我无法判断何时关闭每个线程的 NHibernate 会话以释放用完的资源。
我应该生成自己的线程(以确保它们是唯一的)还是有更好的方法使用线程池来执行此操作?
Since threads are reused over and over by the ThreadPool I can't tell when to close NHibernate sessions for each thread to release used up resources.
Should I spawn my own threads (to ensure they are unique) or there is a better way to do this using the ThreadPool?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我看不出问题所在。您可能需要在问题中详细说明或添加一些代码。
每个线程都有它自己的方法。只需在方法的开头分配会话并在最后清理它即可。当您使用线程池线程时,这同样适用。
不要忘记将所有线程代码包装在 try/catch 中,否则如果未处理异常,您的应用程序将崩溃。
I fail to see the problem. You might to have to elaborate or add some code in your question.
Each thread has it's own method. Simply allocate the session in the beginning of the method and clean it up in the end. The same applies when you are using a thread pool thread.
Don't forget to wrap all thread code in a try/catch, or your application will crash if an exception is unhandled.
我将其设置为每个线程有一个会话。这可能是确保您不会遇到线程终止另一个线程正在使用的会话的问题的最简单方法。
I'd have it set up so that there is one session per thread. This is probably the easiest way to make sure that you don't run into issues where you have a thread terminating a session that is in use by another thread.