我知道 spring 可以为你管理 session ,如果配置如下:
<object name="SessionFactory"
type="MyApp.DAL.DAOs.SessionFactoryObject, Leverate.Crm.CrmService.DAL">
<!--My class which inherits from LocalSessionFactoryObject and defines the mapping assemblies to be used-->
<property name="ExposeTransactionAwareSessionFactory" value="true" />
<property name="DbProvider" ref="DbProvider"/>
<property name="HibernateProperties">
<dictionary>
<entry key="cache.use_second_level_cache" value="false"/>
<entry key="command_timeout" value="180"/>
<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="SchemaUpdate" value="false"/>
</object>
<db:provider id="DbProvider"
provider="SqlServer-1.1"
connectionString="Data Source=local;Initial Catalog=northwind;Trusted_Connection=Yes;"/>
问题是,使用 SessionFactory.GetCurrentSession()
会抛出一个 “没有 Hibernate Session 绑定(bind)到线程,并且配置不允许在此处创建非事务 session ”
异常(exception)。
我看到一些答案说在网络上下文中你必须使用openSessionInView
(或类似的)httpModule。但是您在控制台应用程序(或 Windows 服务)中做什么?
最佳答案
您将 session 工厂公开为事务感知 session 工厂,这意味着 Spring 为您管理正确包装在事务中的所有方法的 session 。为了使用 spring 将数据访问方法包装在事务中,您可以使用 declarative (attribute driven)或programmatic方法。
关于c# - 用于控制台应用程序的 spring .net nhibernate session 管理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9963879/