如何使用Spring.NET/NHibernate的OpenSessionInView支持多数据源?

时间:2012-09-13 05:11:26

标签: asp.net nhibernate spring.net

我从Spring.NET + NHibernate - Multiple (Distinct) Databases with OpenSessionInView找到了一些提示,但它对我不起作用,需要你的帮助来确定出现了什么问题。

我的项目基于ASP.NET /Spring.NET(OpenSessionInView)/ NHibernate,我们需要对外部数据库进行操作,因此我们希望以OSIV模式支持多数据源。

我们通过参考以上链接做了什么:

(1)创建Spring.Data.NHibernate.Support.OpenSessionInViewModule的子类

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

(2)在web.config中配置OSIV

这是我对Spring.NET和NHibernate集成的配置:

<!-- 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>

当我们运行它时,系统说:

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

我们发现任何Repository对象中的SessionFactoryObject都是null。

日志就像这样:

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

糟糕!我们真的被它坚持了。

1 个答案:

答案 0 :(得分:1)

问过你提到的问题&amp;已经使用了基于它的解决方案超过2年,我可以帮助你。

虽然不理想,而不是子类OpenSessionInViewModule,但您需要将其复制from Spring.NET's source。执行此操作时,请确保对默认构造函数进行一次更改。它看起来像这样:

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

基类上调用的第二个参数的类型应该是新复制的类 - 例如:

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

除此之外,你问题上的其他一切似乎都是正确的。让我知道这是怎么回事。