【问题标题】:spring .net nhibernate session management for console application控制台应用程序的spring .net nhibernate会话管理
【发布时间】:2012-04-01 11:05:57
【问题描述】:

我了解 spring 可以为您管理会话,如果配置如下:

<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() 会引发'No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here' 异常。

我看到一些答案说在网络环境中你必须使用openSessionInView(或类似的)httpModule。但是您在控制台应用程序(或 Windows 服务)中做什么呢?

【问题讨论】:

  • 它是一个控制台应用程序,那么它是一个单用户应用程序。为什么要共享会话?
  • 首先-我可能有几个操作/线程要共享同一个会话。其次-我的印象是(如果我错了,请纠正我)我可以纯粹为了方便而使用弹簧,而无需拨打SessionFactory.OpenSession()
  • 如果你只是使用 spring.net 进行会话管理。我建议使用 Ninject
  • 为什么建议使用 Ninject?

标签: c# nhibernate spring.net spring-data


【解决方案1】:

您将会话工厂公开为事务感知会话工厂,这意味着 Spring 为您管理正确包装在事务中的所有方法的会话。要使用 spring 将数据访问方法包装在事务中,可以使用 declarative (attribute driven)programmatic 方法。

【讨论】:

  • 没错。我可能会进一步补充一点,我发现在我的应用程序代码中调用 SessionFactory.OpenSession() 是完全可以接受的。我只需要自己管理会议(无论如何,这不是一项艰巨的任务)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-01
相关资源
最近更新 更多