Spring.NET [Transaction] 属性是否支持多个事务管理器?

发布于 2024-12-31 21:03:39 字数 1056 浏览 4 评论 0 原文

我已经在使用 IBatis 和 TxScopeTransactionManager 的代码库中使用 Spring.NET 声明式事务管理有一段时间了。我刚刚向项目添加了 Spring.NET NHibernate 支持(使用 OSIV 模式),没有出现任何问题。 NHibernate 将仅用于新功能,ADO.NET/IBatis 遗留数据访问代码仍然需要按原样工作。所以我现在在我的上下文中有两个不同的事务管理器:

  1. Spring.Data.Core.TxScopeTransactionManager (现有的 Tran Mgr)
  2. 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 TranRollback 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:

  1. Spring.Data.Core.TxScopeTransactionManager (existing Tran Mgr)
  2. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

压抑⊿情绪 2025-01-07 21:03:39

根据 Java文档 Spring的Java实现中的@Transaction属性有一个附加参数,用于引用要使用的事务管理器。
今天的 spring.net 中不存在这个(我在 1.3.2 版本中没有找到它),但我发现了一个 DelegatingTransactionAttributeWithName 乍一看似乎相关,但我不知道如何使用它。

尽管如此,即使没有,您也可以通过以下方式使用多个 TransactionManager:

  • 通过从 AbstractPlatformTransactionManager 并扩展[Transaction] 模仿 Java 世界已有行为的属性。
  • 使用声明性事务注释而不是基于代码的属性并定义单独的事务管理器。

如果您打算采用后面的路线,则需要:

  • 删除
  • 而是声明两个事务管理器 (一个用于 Nhibernate,一个用于 Ibatis)
  • 为应使用 NhibernateTransactionManager 的方法和应使用 Ibatis TransactionManager 的方法单独声明切入点。声明事务管理器和切入点的方法在文档部分的第二部分17.5.3。在 的声明式事务>17.5.4。交易属性设置

尽管我自己没有尝试过,但这应该有效。同样,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:

  • Implement your own "DelegatingTransactionManager" by deriving from AbstractPlatformTransactionManager and extend the [Transaction] Attribute to mimic the behaviour the Java world alread has.
  • Use declarative transaction annotation instead of code-based attributes and define separate transactionManagers.

If you are going to go the later route you need to:

  • remove <tx:attribute-driven transaction-manager="transactionManager"/>
  • instead declare two transaction managers (one for Nhibernate, one for Ibatis)
  • declare pointcuts individually for the methods which should use the NhibernateTransactionManager and those methods which should use the Ibatis TransactionManager. The way to declare a transaction manager and pointcuts is described in the second part of the documentation section 17.5.3. Declarative transactions using the transaction namespace just before 17.5.4. Transaction attribute settings.

Even though I haven't tried it myself, this should work. Again the Java guys were first and evolved from there...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文