使用 Oracle Streams AQ 在 Spring MDB 中共享 JMS 和 Hibernate 事务?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1223899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sharing JMS and Hibernate transactions in a Spring MDB using Oracle Streams AQ?
提问by otto.poellath
I'm using Oracle 11g for my database and its Oracle Streams AQ feature as JMS implementation.
我将 Oracle 11g 用于我的数据库及其 Oracle Streams AQ 特性作为 JMS 实现。
For all I know, it should be possible to implement a Spring based message-driven POJO (MDP) that uses the same data source for both transactional data access and JMS transactions -- all without XA-Transactions (IIRC, this was marketed as a feature of SpringSource Advanced Pack for Oracle).
据我所知,应该可以实现一个基于 Spring 的消息驱动的 POJO (MDP),它对事务数据访问和 JMS 事务使用相同的数据源——所有这些都没有 XA-Transactions(IIRC,这是作为一个SpringSource Advanced Pack for Oracle 的特性)。
Is this possible using Hibernate as well? Ideally, my MDP would start a JMS transaction and read a message from a queue, then re-use the transaction for data access through Hibernate. If anything goes wrong, the JMS and database transaction would both be rolled back, without using 2-phase commit (2PC).
这也可以使用 Hibernate 吗?理想情况下,我的 MDP 将启动一个 JMS 事务并从队列中读取一条消息,然后通过 Hibernate 重新使用该事务进行数据访问。如果出现任何问题,JMS 和数据库事务都将回滚,而不使用两阶段提交 (2PC)。
I'm not much of a transaction guru, so before I start digging deeper, can anyone confirm that this is possible and makes sense as well?
我不是一个交易大师,所以在我开始深入挖掘之前,有人能确认这是可能的并且也有意义吗?
Update:
What I want is an implementation of the Shared Transaction Resource pattern. The sample codedemonstrates it for ActiveMQ and JDBC, but I need to use Oracle Streams AQ and Hibernate.
更新:
我想要的是Shared Transaction Resource pattern 的实现。该示例代码演示了它的ActiveMQ和JDBC,但我需要使用Oracle流AQ和Hibernate。
Update2:The SpringSource Advanced Pack for Oracle has been open sourcedas part of Spring Data JDBC and it "provides the option of using a single local transaction manager for both database and message access without resorting to expensive distributed 2-phase commit transaction management".
更新 2:SpringSource Advanced Pack for Oracle 已作为 Spring Data JDBC 的一部分开源,它“提供了使用单个本地事务管理器进行数据库和消息访问的选项,而无需求助于昂贵的分布式两阶段提交事务管理”。
回答by skaffman
2PC shouldn't be necessary, as you say, since the appserver should take care of it. However, you'll pretty much have to use JTA (i.e. JavaEE container) transactions, rather than vanilla DataSource transactions, since JMS only works with JTA.
正如您所说,2PC 不是必需的,因为应用程序服务器应该处理它。但是,您几乎必须使用 JTA(即 JavaEE 容器)事务,而不是普通的 DataSource 事务,因为 JMS 仅适用于 JTA。
This isn't a big deal, it's just a bit more fiddly:
这没什么大不了的,只是有点麻烦:
- Your Spring config should use
<jee:jndi-lookup/>
to get a reference to your container'sDataSource
, and you inject that data source into your spring-managed hibernate SessionFactory. - You then need to introduce a transaction manager into the context (
<tx:jta-transaction-manager/>
should work in most app-servers). - In your Spring JMS
MessageListenerContainer
, plug the above transaction manager reference into it.
- 您的 Spring 配置应该用于
<jee:jndi-lookup/>
获取对容器的 的引用DataSource
,然后将该数据源注入到 Spring 管理的休眠 SessionFactory 中。 - 然后你需要在上下文中引入一个事务管理器(
<tx:jta-transaction-manager/>
应该在大多数应用服务器中工作)。 - 在您的 Spring JMS 中
MessageListenerContainer
,将上述事务管理器引用插入其中。
Does that all make sense, or should I elaborate? This setup shouldensure that the container-managed transactions are held across JMS and Hibernate interactions.
这一切都有意义吗,还是我应该详细说明?此设置应确保容器管理的事务跨 JMS 和 Hibernate 交互保持。