如何与TxScopeTransactionManager一起实现会话处理

时间:2012-10-19 09:24:37

标签: nhibernate spring.net

当我使用包含“IList<>”的对象调用我的DAO两次Save()时,我得到异常“非法尝试将集合与两个打开的会话相关联”由“Bag”映射:

public abstract class AbstractGenericDao<TEntity> : HibernateDaoSupport, IAbstractGenericDao<TEntity>
{
  [Transaction]
  public TEntity Save( [NotNull] TEntity entity )
  {
    HibernateTemplate.Save( entity );
  }
}

我正在使用两个SessionFactories

<!-- NHibernate Configuration -->
<object id="NHibernateSessionFactory" abstract="true" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate32">
  <property name="HibernateProperties">
    <dictionary>
      <entry key="connection.provider" value="${connection.provider}" />
      <entry key="connection.driver_class" value="${hibernate.driver}" />
      <entry key="dialect" value="${hibernate.dialect}" />
      <entry key="hbm2ddl.auto" value="${hibernate.hbm2ddl}" />
      <entry key="show_sql" value="${hibernate.show_sql}" />
    </dictionary>
  </property>
</object>

<!-- NHibernate Session Configuration -->
<object id="NHibernateSessionFactory_1" parent="NHibernateSessionFactory" >
  <property name="DbProvider" ref="DbProvider_Main" />
  <property name="MappingAssemblies">
    <list>
      <value>DomainModel</value>
    </list>
  </property>
  <property name="ExposeTransactionAwareSessionFactory" value="true" />
</object>

<!-- NHibernate Session Configuration -->
<object id="NHibernateSessionFactory_2" parent="NHibernateSessionFactory" >
  <property name="DbProvider" ref="DbProvider_Exam" />
  <property name="MappingAssemblies">
    <list>
      <value>DomainModel</value>
    </list>
  </property>
  <property name="ExposeTransactionAwareSessionFactory" value="true" />
</object>

和“TxScopeTransactionManager”:

<object id="transactionManager" type="Spring.Data.Core.TxScopeTransactionManager, Spring.Data">
    <property name="TransactionSynchronization" value="Always"/>
</object>

有人能帮助我吗?

0 个答案:

没有答案