Question

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!

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top