我已经在使用 IBatis 和 TxScopeTransactionManager 的代码库中使用 Spring.NET 声明式事务管理有一段时间了。我刚刚向项目添加了 Spring.NET NHibernate 支持(使用 OSIV 模式),没有出现任何问题。 NHibernate 将仅用于新功能,ADO.NET/IBatis 遗留数据访问代码仍然需要按原样工作。所以我现在在我的上下文中有两个不同的事务管理器:
- Spring.Data.Core.TxScopeTransactionManager (现有的 Tran Mgr)
- Spring.Data.NHibernate.HibernateTransactionManager (为 Nhibernate 添加)
但是,看起来 Spring.NET 只允许单个 TransactionManager绑定到 Transaction 属性,因为用此属性修饰的旧服务方法现在使用 HibernateTransactionManager 而不是 TxScopeTransactionManager 来提交或回滚事务。这是有问题的,因为 HibernateTransactionManager 不知道任何非 nHibernate(读取 ADO.NET)连接。
有没有办法让[Transaction]使用不同的事务管理器,也许与ObjectNameAutoProxyCreator配合以基于命名空间包含/排除服务类?
如果没有,有没有办法让一个 transactionManager 同时处理 NHibernate 和 IBatis 事务?
我使用 HibernateTransactionManager 来管理两种数据访问策略对此进行了测试,但我的 IBatis 事务没有回滚。这特别奇怪,因为我可以在 SQL Profiler 中看到 HibernateTransactionManager 正在发送 Begin Tran 和 Rollback Tran ,但数据仍然被提交。
如果我使用 TxScopeTransactionManager,那么我的 IBatis 事务会成功回滚,但 NHibernate 对数据库的写入永远不会刷新。
I've been using Spring.NET declarative Transaction management for some time in a code base that uses IBatis in conjunction with the TxScopeTransactionManager. I just added Spring.NET NHibernate support to the project (using OSIV pattern) without issue. NHibernate will be used for new features only, ADO.NET/IBatis legacy data access code still needs to work as is. So I now have two distinct transaction managers in my context:
- Spring.Data.Core.TxScopeTransactionManager (existing Tran Mgr)
- Spring.Data.NHibernate.HibernateTransactionManager (added for Nhibernate)
However, it looks like Spring.NET only allows a single TransactionManager to be bound to the Transaction attribute because legacy service methods decorated with this attribute are now using HibernateTransactionManager instead of TxScopeTransactionManager to commit or rollback transactions. This is problematic since HibernateTransactionManager is not aware of any non-nHibernate (read ADO.NET) connections.
Is there a way to have [Transaction] use different Transaction Managers, perhaps in concert with the ObjectNameAutoProxyCreator to either include/exclude service classes based on namespace?
If not, is there a way to have a single transactionManager handle both NHibernate and IBatis transactions?
I tested this using HibernateTransactionManager to manage both Data Access strategies, but my IBatis transactions aren't getting rolled back. This is particularly odd, because I can see in SQL Profiler that both a Begin Tran and Rollback Tran are being sent by HibernateTransactionManager, but the data still gets committed.
If I use TxScopeTransactionManager, then my IBatis transactions rollback successfully but NHibernate writes to the DB are never flushed.
发布评论
评论(1)
根据 Java文档 Spring的Java实现中的
@Transaction
属性有一个附加参数,用于引用要使用的事务管理器。今天的 spring.net 中不存在这个(我在 1.3.2 版本中没有找到它),但我发现了一个
DelegatingTransactionAttributeWithName
乍一看似乎相关,但我不知道如何使用它。尽管如此,即使没有,您也可以通过以下方式使用多个 TransactionManager:
[Transaction]
模仿 Java 世界已有行为的属性。如果您打算采用后面的路线,则需要:
尽管我自己没有尝试过,但这应该有效。同样,Java 人员是第一个,并从那里...
According to the Java documentation the
@Transaction
Attribute in the Java implementation of Spring has an additional parameter which is used to reference the transaction manager to use.This doesn't exist in spring.net today (I haven't found it in version 1.3.2) but I found a
DelegatingTransactionAttributeWithName
which at first sight seems to be related but I don't know how to use it.Nevertheless, even if it doesn't you have the following possibilities to use multiple TransactionManagers:
[Transaction]
Attribute to mimic the behaviour the Java world alread has.If you are going to go the later route you need to:
<tx:attribute-driven transaction-manager="transactionManager"/>
Even though I haven't tried it myself, this should work. Again the Java guys were first and evolved from there...