1

I found some tip from Spring.NET + NHibernate - Multiple (Distinct) Databases with OpenSessionInView, but it dosen't work for me, need your help to identify what's going wrong.

my project is based on ASP.NET/Spring.NET(OpenSessionInView)/NHibernate, and we need to manupilate an external database, so we want to support multi datasources in OSIV pattern.

what we have done by ref the above links:

(1) Create a subclass of Spring.Data.NHibernate.Support.OpenSessionInViewModule

namespace MyProject.Infrastructure.NHibernate
{
    public class ExtDbOpenSessionInViewModule : OpenSessionInViewModule
    {
    }
}

(2) Confige the OSIV in web.config

here is my configurations for Spring.NET and NHibernate integration:

<!-- Spring Web Support and OSIV for NHibernate -->
<add name="Spring" type="Spring.Context.Support.WebSupportModule, Spring.Web"/>
<add name="OpenSessionInViewOnLocalDb" type="Spring.Data.NHibernate.Support.OpenSessionInViewModule, Spring.Data.NHibernate32"/>
<add name="OpenSessionInViewOnExtDb" type="MyProject.Infrastructure.NHibernate.ExtDbOpenSessionInViewModule, MyProject.Infrastructure"/>

<!-- NHibernate SessionFactory in OSIV-->
<appSettings>
    <add key="Spring.Data.NHibernate.Support.OpenSessionInViewModule.SessionFactoryObjectName" value="LocalDbSessionFactory"/>
    <add key="MyProject.Infrastructure.NHibernate.ExtDbOpenSessionInViewModule.SessionFactoryObjectName" value="ExtDbSessionFactory"/>
</appSettings>

When we were running it, the system says:

No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

we found that SessionFactoryObject in any Repository object is null.

the logs was like these:

2012-09-13 10:29:19,017 [13] DEBUG MyProject.Infrastructure.NHibernate.ExtDbOpenSessionInViewModule [(null)] - Trying to close SessionScope
2012-09-13 10:29:19,018 [13] DEBUG MyProject.Infrastructure.NHibernate.ExtDbOpenSessionInViewModule [(null)] - Only participated Hibernate Session - doing nothing
2012-09-13 10:29:19,030 [13] DEBUG MyProject.Infrastructure.NHibernate.ExtDbOpenSessionInViewModule [(null)] - Participating in existing Hibernate SessionFactory
2012-09-13 10:29:19,033 [14] DEBUG MyProject.Infrastructure.NHibernate.ExtDbOpenSessionInViewModule [(null)] - Participating in existing Hibernate SessionFactory
2012-09-13 10:29:19,043 [13] DEBUG MyProject.Infrastructure.NHibernate.ExtDbOpenSessionInViewModule [(null)] - Trying to close SessionScope
2012-09-13 10:29:19,055 [14] DEBUG MyProject.Infrastructure.NHibernate.ExtDbOpenSessionInViewModule [(null)] - Trying to close SessionScope
2012-09-13 10:29:19,062 [13] DEBUG MyProject.Infrastructure.NHibernate.ExtDbOpenSessionInViewModule [(null)] - Only participated Hibernate Session - doing nothing
2012-09-13 10:29:19,071 [14] DEBUG MyProject.Infrastructure.NHibernate.ExtDbOpenSessionInViewModule [(null)] - Only participated Hibernate Session - doing nothing
2012-09-13 10:29:19,117 [13] DEBUG MyProject.Infrastructure.NHibernate.ExtDbOpenSessionInViewModule [(null)] - Participating in existing Hibernate SessionFactory
2012-09-13 10:29:19,122 [13] DEBUG MyProject.Infrastructure.NHibernate.ExtDbOpenSessionInViewModule [(null)] - Trying to close SessionScope
2012-09-13 10:29:19,126 [13] DEBUG MyProject.Infrastructure.NHibernate.ExtDbOpenSessionInViewModule [(null)] - Only participated Hibernate Session - doing nothing
2012-09-13 10:29:19,130 [13] DEBUG MyProject.Infrastructure.NHibernate.ExtDbOpenSessionInViewModule [(null)] - Participating in existing Hibernate SessionFactory
2012-09-13 10:29:19,132 [13] DEBUG MyProject.Infrastructure.NHibernate.ExtDbOpenSessionInViewModule [(null)] - Trying to close SessionScope
2012-09-13 10:29:19,132 [13] DEBUG MyProject.Infrastructure.NHibernate.ExtDbOpenSessionInViewModule [(null)] - Only participated Hibernate Session - doing nothing
2012-09-13 10:29:21,347 [14] DEBUG MyProject.Infrastructure.NHibernate.ExtDbOpenSessionInViewModule [(null)] - Participating in existing Hibernate SessionFactory
2012-09-13 10:29:21,441 [14] INFO  Spring.Aop.Framework.AutoProxy.InfrastructureAdvisorAutoProxyCreator [(null)] - Candidate advisor [ObjectFactoryTransactionAttributeSourceAdvisor: advice object 'Spring.Transaction.Interceptor.TransactionInterceptor#0'] rejected for targetType [ASP.pages_sysmgmt_debug_aspx]
2012-09-13 10:29:21,457 [14] DEBUG MyProject.Infrastructure.NHibernate.ExtDbOpenSessionInViewModule [(null)] - Trying to close SessionScope
2012-09-13 10:29:21,458 [14] DEBUG MyProject.Infrastructure.NHibernate.ExtDbOpenSessionInViewModule [(null)] - Only participated Hibernate Session - doing nothing

Oops! we are realy stuck by it.

Community
  • 1
  • 1
Steven Wang
  • 145
  • 9
  • This [question and answers](http://stackoverflow.com/questions/10981625/nhibernate-spring-net-lazy-loading-failed-no-session/10982363#10982363) might be of help. – Marijn Sep 13 '12 at 06:19

1 Answers1

1

Having asked the question you refer to & have used a solution based on it for over 2 years, I may be able to help you.

While not ideal, rather than subclass OpenSessionInViewModule, you'll need to copy it from Spring.NET's source. When doing this, make sure you make one alteration to the default constructor. It will look something like this:

public MyOpenSessionInViewModule() : 
       base("appSettings", typeof(OpenSessionInViewModule), false)

The type on the second parameter to the call on the base class should be your newly copied class - for example:

public MyOpenSessionInViewModule() : 
       base("appSettings", typeof(MyOpenSessionInViewModule), false)

Other than that, everything else on your question seems to be correct. Let me know how this goes.

Simon Rice
  • 1,119
  • 1
  • 13
  • 22