NHibernate transaction management in ASP.NET MVC - how to do it?

I am writing a simple ASP.NET MVC using a session for each request and transaction for each request template (custom HttpModule). It seems to work fine, but .. the performance is terrible (a simple page loads ~ 7 seconds). For each HTTP request, graphic resources, including (all images on the site), create a transaction and seem to delay the download time (without transaction loading time, a single image is ~ 1-10 ms with transactions that take more than 1 second), What is the right way to manage transactions on an ASP.NET MVC + NH stack?

When I put all the transactions in my repository methods, for some unclear reasons, I received a warning about implicit transactions in NHProf (SQL statements were executed outside the transaction, even in the code session .Save () / Update () / etc were called in the transaction using scope and before transaction. commit () BTW call - are implicit transactions really bad?

+7
design-patterns asp.net-mvc nhibernate transactions
source share
3 answers

You need to use a context session so that a new session and transaction are not created every time you request an image resource, etc. See my answer to this question . This change should significantly improve performance. This is probably also the main reason for implicit transaction alerts.

Another common problem with NH application performance is using log4net with the DEBUG level.

Edit: Min and Kevin Pang answers are also good advice, so I will not repeat their statements.

+3
source share

Implicit transactions occur when you do not place your statements in a transaction. Implicit is that if you do not explicitly declare a transaction, each of your statements operates in its own transaction. So bad. This is bad? I don’t know enough about what you are working on to judge.

I do the same thing you do with a session per request, but I do not approach the same performance numbers. I would recommend looking at enabling log4net for NHibernate and seeing what it does. Your application needs to create a factory session and proxy plants once (managing a factory session will be one of the few places in which I would use a single singleton). This is really the only thing I can think of that can cause this.

+4
source share

There are a couple of questions here:

Firstly, regarding your performance issues. Are you sure that ISessionFactory is created only once? This is a very expensive object to create, so it should probably be a singleton in your web application created on Application_Start and not in Application_BeginRequest. My guess about why you get such poor performance is that you create a new ISessionFactory for each request, and not once for the entire application.

Secondly, with regard to implicit transactions, they are not so bad, but because of how you describe your code, you should not get these errors. Are you sure you are making your calls within a transaction? You may be gaining access to some lazy loading properties from .aspx and .ascx pages that may run outside of your transaction.

+4
source share

All Articles