在Spring .NET中更改为singleton会停止NHibernate中的更新吗?(Change to singleton in Spring .NET stops updates in NHi

编程入门 行业动态 更新时间:2024-10-23 05:42:38
在Spring .NET中更改为singleton会停止NHibernate中的更新吗?(Change to singleton in Spring .NET stops updates in NHibernate?)

我的Web应用程序遇到了一个奇怪的问题。

让我解释一下我的应用程序设置 - 应用程序是使用IIS 7.x中托管的asp .net mvc 3进行的。我使用Spring .NET来实例化我的控制器,业务层和存储库。 存储库使用NHibernate 3.3。 每个应用程序创建一次NHibernate会话工厂(由Spring .NET控制)。 在Global.asax(开始请求)中创建新会话并将其绑定到每个请求的上下文。 会话从End请求中的会话上下文中解除绑定。 显然,有一些检查可以确保只针对有效的操作方法而不是每个请求。

业务层注入了存储库类和NHibernate ISession周围的自定义包装器。 此类中的函数执行必要的DB操作。 创建/更新/删除操作始终在NHibernate事务中。 这个应用程序的过去几个月已经很好用了。

我在Spring .NET XML配置中将业务层类标记为singleton = false。 这意味着对于每个请求,将创建一个新的控制器实例和业务层及其依赖关系的新实例。 在进行了一些性能分析之后,我发现在CPU周期方面这是一个相当昂贵的操作。 所以,我切换到将业务层类标记为singleton = true。 这降低了通过MVC3 GetService()调用重新创建业务层对象所需的CPU周期。

问题 -将业务层类更改为单例后,不再提交数据库更新。 Create actions are persisted to the DB. But not updates. 我通过NHProfiler查看了NHibernate会话活动,我看到了这个:

(用于创建操作)

begin transaction select * from table_x insert into table table_y .... commit end transaction

(用于更新操作)

begin transaction end transaction

就是这样,只是一个开始和结束的交易。 事务的内容永远不会被刷新。 更新该会话的内存中的NHibernate实体。 ISession.WasCommitted propery返回true ..一切正常,会话绑定到该请求的上下文(然后对该请求解除绑定),其他一切正常发生。 有趣的是, 如果有插入操作,则刷新到DB(但只有插入) 。 但是,当更新也发生时,该操作不会在NHProfiler中显示。

如果我将该业务层对象的单例属性(在Spring .NET上下文配置XML中)切换回false,则一切正常。 所有CRUD操作都像以前一样继续。

那么,我可能做错了什么? 任何帮助/建议将不胜感激。

-谢谢!

I'm facing an odd problem with my web application.

Let me explain my application setup - The application is made with asp .net mvc 3 hosted in IIS 7.x .I use Spring .NET for instantiating my controllers, business layer and repositories. The repository uses NHibernate 3.3. The NHibernate session factory is created once per application (controlled with Spring .NET). A new session is created and bound to a context per request in Global.asax (begin request). The session is unbound from the session context in End request. Obviously, there are checks to ensure that this is done only for a valid action method and not every request.

The business layer is injected with repository classes and a custom wrapper around an NHibernate ISession. Functions inside this class perform necessary DB operations. The create/update/delete actions are always inside an NHibernate transaction. This has worked out well for the past few months of this application.

I was marking my business layer classes as singleton=false in Spring .NET XML configuration. This meant that for each request, a new controller instance and a new instance of the business layer and its dependencies would be created. After doing some performance profiling, I've found that this was a fairly expensive operation in terms of CPU cycles. So, I switched to marking my business layer classes to singleton=true. This brought down the CPU cycles needed to recreate business layer objects via the MVC3 GetService() call.

The problem - after changing the business layer class to singletons, DB updates are no longer committed. Create actions are persisted to the DB. But not updates. I took a look at the NHibernate session activity via NHProfiler and what I see this :

(for a create operation)

begin transaction select * from table_x insert into table table_y .... commit end transaction

(for an update operation)

begin transaction end transaction

And that's it, just a begin and end transaction. The contents of the transaction are never flushed. NHibernate entities in memory for that session are updated. The ISession.WasCommitted propery returns true.. everything reads fine, the session is bound to a context for that request (and then unbound for that request), everything else happens normally. Interestingly, if there's an insert operation, that is flushed to the DB (but only the insert). However, that operation doesn't show in NHProfiler when an update also happens.

If I toggle the singleton property (in the Spring .NET context config XML) for that business layer object back to false, everything works normally. All CRUD operations continue as before.

So, what could I be doing wrong? Any help/suggestions would be appreciated.

-Thanks!

最满意答案

问题在于我自己的代码。 我正在坚持错误的会议。

The problem was with my own code. I was holding on to the wrong session.

更多推荐

本文发布于:2023-08-07 05:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1461247.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:singleton   NET   Spring   NHibernate   updates

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!