Lodewijk
Lodewijk

Reputation: 2401

Session-in-view and TransactionScope

we have a asp.net webforms app using NHibernate. Here are some specifics:

So basically, what happens is this (pseudocode):

using(var session = sessionFactory.CreateSession()){
  using(var tx1 = new TransactionScope(){
    //work work work
    tx1.Complete();
  }

  //other work

  using(var tx2 = new TransactionScope(){
    //work work work
    tx2.Complete();
  }
}

However we now get into a situation where we see a lot of crashes related to the Connection to the database. Some researching gave us two suggestions:

However, we have two questions about these suggestions:

  1. Doesn't NHibernate automatically enlist itself into the TransactionScope. Why do we need to create a transaction explicitly for NHibernate?
  2. If we must create our Session within the TransactionScope, how can we combine this with the Session-in-view pattern?

Upvotes: 3

Views: 402

Answers (2)

DanP
DanP

Reputation: 6478

Have a look at the ncommon framework, it demonstrates a method of using NHibernate with a TransactionScope using the concept of a UnitOfWorkScope; this may be the guidance that you're looking for.

Upvotes: 0

Diego Mijelshon
Diego Mijelshon

Reputation: 52745

  • You must use NH transactions (session.BeginTransaction())
  • NH transactions will automatically enlist in the TransactionScope
  • You don't have to create your session inside the TransactionScope.

Upvotes: 4

Related Questions