views:

295

answers:

1

Hi all,

I am currently working on an enterprise solution, which hosts several applications and due to that I implemented NHibernate to work with several session factories. I created a custom section like:

  <hibernate-sessionFactories>
    <sessionFactories>
      <clear/>
      <add name="Server1" factoryConfigPath="~/SessionFactoryConfigurations/Server1.config"/>
      <add name="Server2" factoryConfigPath="~/SessionFactories/Server2.config" />
    </sessionFactories>
  </hibernate-sessionFactories>

I implemented a lazy singleton session provider which instantiates these session factories on demand.

I am using the repository pattern. Within a persistence method, to get the current NHibernate session, I now can call something like

ISession session = sessionProvider.GetCurrentSessionFrom(sessionFactoryConfigName);

So depending on the session factory config name I get the correct session. For transactional things I would like to use the System.Transactions.TransactionScope, so I can have "nested" transactions like:

using(var scope = new TransactionScope(System.Transactions.TransactionScopeOption.Required))
{
  using(var scope = new TransactionScope(System.Transactions.TransactionScopeOption.Required))
  {
  }
}

Would that work without problems? Since I'm working with multiple server, how does the transaction scope know on which server to enlist the transaction? Are there better ways to do transaction management with easy handling of nested transactions?

Do you understand my question anyway? ;)

A: 

TransactionScope will determine this automatically based on the Ado.Net connections actually used. This should get you two-phase commit between your database servers (via MSDTC), if they support it AND you enabled/configured your environment to support global transactions between the application server and database servers.

I prefer to use Spring.Net to manage transactions in a declarative fashion. But I have to admit I have not used it with multiple NHibernate session factories.

Fried Hoeben