Accessing current NHibernate session / transaction from controller?

Topics: General, Writing modules
Jul 25, 2011 at 8:33 PM

Hi All,

I'm calling Uncle.  Anyone know a way to get a hold of the current NHibernate session and / or transaction within a module controller?  I want to perform some database manipulation (create database, create database user, etc.) that's outside the Repository's responsibility.  If I just try to create a new NHibernate session, I get a MySQL error "Multiple simultaneous connections or connections with different connection strings inside the same transaction are not currently supported.".  I'm happy to use the transaction that is opened with the controller action, but I can't seem to find where that is. 

I've tried injecting an ISessionFactoryHolder and opening a new session like this:

 

IQuery sqlQuery = _sessionFactoryHolder.GetSessionFactory().OpenSession().CreateSQLQuery(sql);

But I get the simultaneous connections error.  _sessionFactoryHolder.GetSessionFactory().GetCurrentSession().CreateSQLQuery(sql) give me a "No CurrentSessionContext configured (set the property current_session_context_class)!" error.

I am using MySQL as my database provider (utilizing the gallery module).

Any ideas?  Am I going about this the correct way, or is there another facility on Orchard that I should be utilizing?

Thanks!

 

Coordinator
Jul 25, 2011 at 8:55 PM

Opt out of the ambient transaction: using (new TransactionScope(TransactionScopeOption.Suppress)) {} around your code.

Jul 25, 2011 at 9:41 PM

That was the trick.  Dead on as always!  Big thanks.