ECM
ECM
总版主
总版主
  • 社区居民
  • 忠实会员
  • 原创写手
阅读:1006回复:0

Spring.NET with NHibernate and quartz transaction (global transaction manager)

楼主#
更多 发布于:2016-05-16 10:53
I want to use Global transaction manager at my service layer. eg. namespace AssemblyName.Core.Service.Implementation
{
    public class DemoService
    {

      public void demo()
      {
           save(model); //This is nHibernate transaction
           SchedulerManager.GetInstance.save(id); //This is related to quartz.  
      }
    }      
}
 What should I use? If I used TransactionScope() then it is giving me error as NHibernateTransaction can't be committed. I have used <object id="transactionManager"
    type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate33">

    <property name="DbProvider" ref="DbProvider"/>
    <property name="SessionFactory" ref="NHibernateSessionFactory"/>

</object>
 in my sprin.config file. Edited: Then I have used two transaction manager in spring.config file: <object id="transactionManager"type="Spring.Data.NHibernate.HibernateTransactionManager,Spring.DataNHibernate33">
        <property name="DbProvider" ref="DbProvider"/>
        <property name="SessionFactory"ref="NHibernateSessionFactory"/>
 </object>

<tx:advice id="txAdvice" transaction-manager="transactionManager">
     <tx:attributes>
        <tx:method name="*"/>
     </tx:attributes>
</tx:advice>

<object id="serviceOperation" type="Spring.Aop.Support.SdkRegularExpressionMethodPointcut,Spring.Aop">
      <property name="pattern" value="AssemblyName.Core.Service.Implementation.*"/>
 </object>



<object id="transactionManagerGLobal" type="Spring.Data.Core.TxScopeTransactionManager, Spring.Data">
 </object>

<tx:advice id="txAdviceGlobal" transaction-manager="transactionManagerGLobal">
    <tx:attributes>
       <tx:method name="demo"/>
    </tx:attributes>
</tx:advice>

<object id="serviceOperationGlobal" type="Spring.Aop.Support.SdkRegularExpressionMethodPointcut, Spring.Aop">
    <property name="pattern" value="AssemblyName.Core.Service.Implementation.DemoService"/>
 </object>


<aop:config>
    <aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice"/>
    <aop:advisor pointcut-ref="serviceOperationGlobal" advice-ref="txAdviceGlobal"/>
</aop:config>
 Then also getting Error as: NHibernate Transaction is disconnected or not connected.
answer(解决方案):

Finally, I resolved my issue as following changes: I have added tx annotation for NHibernate transaction : <tx:attribute-driven transaction-manager="transactionManager" proxy-target-type="true"/>
 For Global Transaction I have changed my spring.config file as following and removed advisor 'serviceOperation' of NHibernate Transaction.: <aop:config>
<aop:advisor pointcut-ref="serviceOperationGlobal" advice-ref="txAdviceGlobal"/>
 Changes in my code: namespace AssemblyName.Core.Service.Implementation
{          
      public class DemoService
      {
         [Transaction(TransactionPropagation.Required)]
         public void demo()
         {
          save(model); //This is nHibernate transaction
          SchedulerManager.GetInstance.save(id); //This is related to quartz.
         }
       }      
}
 Edited: By doing this Other problem introduced. It is running fine on many pc but in some pc it shows error : Connection was disconnected or not open, when I do operation related with demo(). I have configured all settings that need to run MS DTC.

 

 

 

 

 

 

 

异常中心网是一家专门收集整理程序员编程过程中遇到的常见异常(exception)以及各种异常问答中心的网站。异常中心网旨在,减少程序员在编码遇到异常,处理各种异常时间和痛苦,让程序员能更愉快的、快速的定位异常并查找对应的异常解决方案。异常中心网诚心打造最完美的编程社区为程序员用户服务,努力成为最好的程序员乐园程序员社区程序异常中心程序bug中心异常问答中心

 

喜欢0 评分0
游客

返回顶部