我正在尝试使用spring.net和nihibernate作为我的数据层。
我有一个简单的DAO对象,其中包含以下代码:
[Transaction]
public long Save(Request entity)
{
return (long)CurrentSession.Save(entity);
}
每当调用此代码时,我都会收到以下错误:
“没有Hibernate会话绑定到线程,并且配置不允许在此处创建非事务性的”
我的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引用为程序集资源。
将此DOA注入到另一个弹簧配置对象中,从中调用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)
答案 0 :(得分: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,&amp;所以你可能还需要将它添加到web.config中:
<appSettings>
<!-- Other App Settings -->
<add key="Spring.Data.NHibernate.Support.OpenSessionInViewModule.SessionFactoryObjectName" value="NHibernateSessionFactory"/>
</appSettings>