问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/23210973/spring-net-with-nhibernate-and-quartz-transaction-global-transaction-manager