Spring.net与NHibernate:解决“No Hibernate Session bound to thread error”错误

5

我正试图使用spring.net和nhibernate来构建我的数据层。

我有一个简单的DAO对象,其中包括以下代码:

[Transaction]
public long Save(Request entity)
{
   return (long)CurrentSession.Save(entity);    
}

每当调用此代码时,我都会收到以下错误提示:
"No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here"
我的DAO层具有以下配置,并在我的web.config中引用:
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"
         xmlns:tx="http://www.springframework.net/tx"
         xmlns:db="http://www.springframework.net/database"
         xmlns:aop="http://www.springframework.net/aop"
         >

  <!-- Referenced by main application context configuration file -->
  <description>
    The Northwind object definitions for the Data Access Objects.
  </description>

  <!-- Property placeholder configurer for database settings -->
  <object type="Spring.Objects.Factory.Config.PropertyPlaceholderConfigurer, Spring.Core">
    <property name="ConfigSections" value="databaseSettings"/>
  </object>

  <!-- Database and NHibernate Configuration -->
  <db:provider id="DbProvider"
                   provider="SqlServer-2.0"
                   connectionString="Data Source=ME-LT;Initial Catalog=SupplyAndDemand;Integrated Security=True"/>

  <object id="NHibernateSessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate21">
    <property name="DbProvider" ref="DbProvider"/>
    <property name="MappingAssemblies">
      <list>
        <value>SAD.Providers.NHibernate</value>
      </list>
    </property>
    <property name="HibernateProperties">
      <dictionary>

        <entry key="connection.provider"
               value="NHibernate.Connection.DriverConnectionProvider"/>

        <entry key="dialect"
               value="NHibernate.Dialect.MsSql2005Dialect"/>

        <entry key="connection.driver_class"
               value="NHibernate.Driver.SqlClientDriver"/>

      </dictionary>
    </property>

    <property name="ExposeTransactionAwareSessionFactory" value="true" />
  </object>


  <object id="transactionManager"
        type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate21">
    <property name="DbProvider" ref="DbProvider"/>
    <property name="SessionFactory" ref="NHibernateSessionFactory"/>

  </object>

  <!-- Data Access Objects -->
  <object id="RequestDao" type="SAD.Providers.Nhibernate.NHibernateRequestDao, SAD.Providers.Nhibernate">
    <property name="SessionFactory" ref="NHibernateSessionFactory" />
  </object>


<tx:attribute-driven transaction-manager="transactionManager"/>

</objects>

在我的web.config文件中,我已经包含了解析器的引用:

<parser type="Spring.Transaction.Config.TxNamespaceParser, Spring.Data"/>

我引用了我的dao.xml作为一个程序集资源。

这个DAO被注入到另一个已配置的Spring对象中,然后调用Save方法。

你有什么想法是我做错了什么吗?如你所见,我包含了配置文件。

<tx:attribute-driven transaction-manager="transactionManager"/> 

我将代码附加到了Spring代码中,并在TransactionInterceptor的invoke方法中放置了一个断点 - 但该方法从未被调用 - 所以我的DAO对象可能没有被创建代理?

以下是完整的堆栈跟踪:

<StackTrace><![CDATA[at Spring.Data.NHibernate.SpringSessionContext.CurrentSession() in F:\Spring.NET\Spring.NET\src\Spring\Spring.Data.NHibernate12\Data\NHibernate\SpringSessionContext.cs:line 70
   at NHibernate.Impl.SessionFactoryImpl.GetCurrentSession()
   at sad.Providers.Nhibernate.NHibernateDao.get_CurrentSession() in F:\PersonnalProjects\sad\trunk\src\sad\sad.Providers.Nhibernate\nHibernateDao.cs:line 29
   at sad.Providers.Nhibernate.NHibernateRequestDao.Save(Request entity) in F:\PersonnalProjects\sad\trunk\src\sad\sad.Providers.Nhibernate\NHibernateRequestDao.cs:line 41
   at sad.Messaging.RequestManager.RequestManager.ProcessRequest(UserCredentials userCredentials, Request request) in F:\PersonnalProjects\sad\trunk\src\sad\sad.Messaging.RequestManager\RequestManager.cs:line 39
   at sad.Messaging.UI.Web.RequestManagerService.ProcessRequest(UserCredentials userCredentials, Request request) in F:\PersonnalProjects\sad\trunk\src\sad\sad.Messaging.UI.Web\RequestManagerService.cs:line 28
   at requestManagerService.ProcessRequest(UserCredentials userCredentials, Request request)
   at SyncInvokeProcessRequest(Object , Object[] , Object[] )
   at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)
1个回答

2
我知道这是一个老问题,但你是否在web.config文件中定义了Open Session In View模块? 对于IIS6,应该像下面这样:
<httpModules>
    <!-- Other modules here -->
    <add name="OpenSessionInView" type="Spring.Data.NHibernate.Support.OpenSessionInViewModule, Spring.Data.NHibernate21"/>
</httpModules>

还有,我可能错了,但我认为Open Session In View会查找一个名为SessionFactory的会话工厂(你猜对了),所以你也需要将其添加到web.config中:

<appSettings>
    <!-- Other App Settings -->
    <add key="Spring.Data.NHibernate.Support.OpenSessionInViewModule.SessionFactoryObjectName" value="NHibernateSessionFactory"/>
</appSettings>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接