具有NHibernate和Crystal事务的Spring.NET(全局事务管理器) [英] Spring.NET with NHibernate and quartz transaction (global transaction manager)
问题描述
我想在我的服务层使用Global Transaction Manager.
I want to use Global transaction manager at my service layer.
例如
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.
}
}
}
我应该使用什么?
如果我使用TransactionScope()
,则由于无法执行NHibernateTransaction,它给了我错误.
If I used TransactionScope()
then it is giving me error as NHibernateTransaction can't be committed.
我用过
<object id="transactionManager"
type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate33">
<property name="DbProvider" ref="DbProvider"/>
<property name="SessionFactory" ref="NHibernateSessionFactory"/>
</object>
在我的sprin.config文件中.
in my sprin.config file.
然后我在spring.config文件中使用了两个事务管理器:
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>
然后还会收到Error: NHibernate事务已断开连接或未连接.
Then also getting Error as: NHibernate Transaction is disconnected or not connected.
推荐答案
最后,我通过以下更改解决了我的问题:
Finally, I resolved my issue as following changes:
我为NHibernate事务添加了tx注释:
I have added tx annotation for NHibernate transaction :
<tx:attribute-driven transaction-manager="transactionManager" proxy-target-type="true"/>
对于全局事务,我已经如下更改了spring.config文件,并删除了NHibernate Transaction的顾问'serviceOperation'.
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"/>
我的代码中的更改:
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.
}
}
}
通过这样做,引入了其他问题. 在许多PC上运行良好,但在某些PC上却显示错误:当我执行与demo()相关的操作时,连接已断开或未打开.
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().
我已经配置了运行MS DTC所需的所有设置.
I have configured all settings that need to run MS DTC.
这篇关于具有NHibernate和Crystal事务的Spring.NET(全局事务管理器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!