问题描述
我使用Spring.NET AOP进行NHibernate事务和会话管理。当用户提出几个请求太快 - 懒惰加载失败,异常没有会议或会议被关闭。我在NHibernate配置中使用SpringSessionContext作为CurrentSessionContext
public class FluentSessionFactory :LocalSessionFactoryObject
{
保护覆盖ISessionFactory NewSessionFactory(配置配置)
{
var conf =可以执行
.Configure()
.Database(
MsSqlConfiguration
.MsSql2008
.ConnectionString(c => c.FromConnectionStringWithKey(MyConnection))
// TODO:使用ExposeConfiguration方法
.CurrentSessionContext< SpringSessionContext>()
.Mappings(
m => m.FluentMappings
.AddFromAssembly(this.GetType()。Assembly)
)
.BuildSessionFactory() ;
返回conf;
$ b $在xml配置中:
< object id =SessionFactorytype =IndustryTracker.NHibernateRepository.FluentSessionFactory,IndustryTracker.NHibernateRepository>
< property name =DbProviderref =DbProvider/>
< / object>
和OpenSessionInView模块
< system.webServer>
< validation validateIntegratedModeConfiguration =false/>
< modules runAllManagedModulesForAllRequests =true>
< add name =OpenSessionInViewtype =Spring.Data.NHibernate.Support.OpenSessionInViewModule,Spring.Data.NHibernate31/>
< / modules>
< /system.webServer>
$ b $ p
$ b 应用程序实现下一个从db:View - > Controller - > Manager - > Repository获取实体的工作流,与其他方面一样。因此,会话创建每个请求,事务 - 每次调用管理器。
< object id =TransactionManagertype =Spring .Data.NHibernate.HibernateTransactionManager,Spring.Data.NHibernate31>
< property name =DbProviderref =DbProvider/>
< property name =SessionFactoryref =SessionFactory/>
< / object>
< tx:advice id =TxAdvicetransaction-manager =TransactionManager>
< tx:attributes>
< tx:method name =*/>
< / tx:属性>
< / tx:advice>
< object id =Pointcuttype =Spring.Aop.Support.SdkRegularExpressionMethodPointcut,Spring.Aop>
< property name =patterns>
< list>
< value> MyAppication.Managers.AccountManager< / value>
< value> MyAppication.Managers.CompanyManager< / value>
< / list>
< / property>
< / object>
< aop:config>
< / aop:config>
这种行为有什么可能的原因,我怎样才能解决这个问题(Not.LazyLoad()和NHibernateUtil.Initialize()在我的上下文中是不可接受的变体)?
解决方案 会话工厂配置为 OpenSessionInViewModule
?
您可能忘记为会话工厂配置 OpenSessionInViewModule
:
< appSettings>
< add key =Spring.Data.NHibernate.Support.OpenSessionInViewModule.SessionFactoryObjectName
value =SessionFactory/>
< / appSettings>
必须在应用程式设定中完成。
。2。正确的FluentNHibernate为基础的春季会话工厂?
您似乎正在代码中配置您的会话工厂。您是否尝试过配置。
I use Spring.NET AOP for transaction and session management with NHibernate. When user makes several requests too quick - lazy loading is failed with exception "no session or session was closed".
I use SpringSessionContext as CurrentSessionContext in NHibernate configuration
public class FluentSessionFactory : LocalSessionFactoryObject
{
protected override ISessionFactory NewSessionFactory(Configuration config)
{
var conf = Fluently
.Configure()
.Database(
MsSqlConfiguration
.MsSql2008
.ConnectionString(c => c.FromConnectionStringWithKey("MyConnection"))
// TODO: use ExposeConfiguration method
.CurrentSessionContext<SpringSessionContext>()
)
.Mappings(
m => m.FluentMappings
.AddFromAssembly(this.GetType().Assembly)
)
.BuildSessionFactory();
return conf;
}
}
In xml config:
<object id="SessionFactory" type="IndustryTracker.NHibernateRepository.FluentSessionFactory, IndustryTracker.NHibernateRepository">
<property name="DbProvider" ref="DbProvider" />
</object>
and OpenSessionInView module
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules runAllManagedModulesForAllRequests="true">
<add name="OpenSessionInView" type="Spring.Data.NHibernate.Support.OpenSessionInViewModule, Spring.Data.NHibernate31"/>
</modules>
</system.webServer>
Application implements next workflow for getting entities from db: View -> Controller -> Manager -> Repository and same to other side. So session is created per request, transaction - per call to manager.
<object id="TransactionManager" type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate31">
<property name="DbProvider" ref="DbProvider"/>
<property name="SessionFactory" ref="SessionFactory"/>
</object>
<tx:advice id="TxAdvice" transaction-manager="TransactionManager">
<tx:attributes>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<object id="Pointcut" type="Spring.Aop.Support.SdkRegularExpressionMethodPointcut, Spring.Aop">
<property name="patterns">
<list>
<value>MyAppication.Managers.AccountManager</value>
<value>MyAppication.Managers.CompanyManager</value>
</list>
</property>
</object>
<aop:config>
<aop:advisor advice-ref="TxAdvice" pointcut-ref="Pointcut"/>
</aop:config>
What are possible reasons of such behaviour and how can I solve this problem(Not.LazyLoad() and NHibernateUtil.Initialize() are not acceptable variants in my context)?
解决方案 1. Session factory configured for OpenSessionInViewModule
?
You might have forgotten to configure the session factory for the OpenSessionInViewModule
:
<appSettings>
<add key="Spring.Data.NHibernate.Support.OpenSessionInViewModule.SessionFactoryObjectName"
value="SessionFactory"/>
</appSettings>
This has to be done in the app settings.
2. Correct FluentNHibernate based spring session factory?
You seem to be configuring your session factory in code. Have you tried configuring the session factory like described in the docs and on BennyM 's blog? Your NewSessionFactory
method returns a session factory from straight from Fluent NHibernate, bypassing all spring.net support.
3. Is your session factory transaction aware?
<object id="SessionFactory"
type="IndustryTracker.NHibernateRepository.FluentSessionFactory, IndustryTracker.NHibernateRepository">
<property name="DbProvider" ref="DbProvider" />
<!-- provides integation with Spring's declarative transaction management features -->
<property name="ExposeTransactionAwareSessionFactory" value="true" />
</object>
4. Does your controller have dependencies with scope="application"
or no scope definition?
I might have been looking in the wrong direction here. If your controller has dependencies of application
scope, it would mean that quick requests could interfere. The default is "scope="application"
; so you'd want to check collaborators without scope definitions too. See the docs on web scopes.
这篇关于NHibernate + Spring.NET延迟加载失败 - 没有会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!