JMSTemplate中的setSessionTransacted到底是什么意思? [英] What precisely means setSessionTransacted in JMSTemplate?

查看:858
本文介绍了JMSTemplate中的setSessionTransacted到底是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我对Spring文档的理解正确,请向我解释.

Please explain me if I understood correctly Spring documentation.

Spring docs指出: https ://docs.spring.io/spring/docs/current/spring-framework-reference/integration.html#jms-tx

Spring docs states: https://docs.spring.io/spring/docs/current/spring-framework-reference/integration.html#jms-tx

(...)在非托管环境中使用JmsTemplate时,可以通过使用sessionTransacted和sessionAcknowledgeMode属性来指定这些值(事务和确认模式).

当将PlatformTransactionManager与JmsTemplate一起使用时,始终为该模板提供一个事务性JMS会话.(..)

When you use a PlatformTransactionManager with JmsTemplate, the template is always given a transactional JMS Session.(..)

(顺便说一句,这是正确的-会话是事务性的)

Javadoc状态:

Javadoc states : https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/jms/core/JmsTemplate.html

JMS会话的默认设置为未交易",默认设置为不交易".和自动确认".根据Java EE规范的定义,当在活动事务中创建JMS会话时,无论JTA事务还是 Spring管理的事务,事务和确认参数都将被忽略.

Default settings for JMS Sessions are "not transacted" and "auto-acknowledge". As defined by the Java EE specification, the transaction and acknowledgement parameters are ignored when a JMS Session is created inside an active transaction, no matter if a JTA transaction or a Spring-managed transaction.

我了解到,如果事务处于活动状态,则JMS模板会话的事务设置将被忽略-这是真的-并且会话应参与活动事务- 这不是事实 .

I understood that if transaction is active, JMS Template session transaction settings are ignored - that is true - and the session should participate active transaction - that is not true.

我调试了为什么它不是真的,然后发现: https://github.com/spring-projects/spring-framework/blame/master/spring-jms/src/main/java/org/springframework/jms/connection/ConnectionFactoryUtils.java#L353

I debug why it is not true and I found: https://github.com/spring-projects/spring-framework/blame/master/spring-jms/src/main/java/org/springframework/jms/connection/ConnectionFactoryUtils.java#L353

if (resourceHolderToUse != resourceHolder) {
  TransactionSynchronizationManager.registerSynchronization(
    new JmsResourceSynchronization(resourceHolderToUse, connectionFactory,
                             
 resourceFactory.isSynchedLocalTransactionAllowed()));
 resourceHolderToUse.setSynchronizedWithTransaction(true);
 TransactionSynchronizationManager.bindResource(connectionFactory, resourceHolderToUse);
}

resourceHolderToUse.setSynchronizedWithTransaction(true)行是对齐文档.

此处的问题:resourceFactory.isSynchedLocalTransactionAllowed()

因为resourceFactory是org.springframework.jms.core.JmsTemplate.JmsTemplateResourceFactory#isSynchedLocalTransactionAllowed,它指向JmsTemplate#sessionTransacted.

Because resourceFactory is org.springframework.jms.core.JmsTemplate.JmsTemplateResourceFactory#isSynchedLocalTransactionAllowed which points to JmsTemplate#sessionTransacted.

结论: 根据文档,如果事务处于活动状态,则应忽略JmsTemplate#sessionTransacted.但这不是事实-尽管会话是事务性的,但不能参与提交.

Conclusion: According to documentation, if transaction is active, JmsTemplate#sessionTransacted should be ignored. But it is not true - although session is transactional, cannot not participate in commit.

JmsTemplate#sessionTransacted最终映射到ConnectionFactoryUtils.JmsResourceSynchronization#transacted,并且default = false防止在事务结束时调用提交(JmsResourceSynchronization认为"它不参与事务)

JmsTemplate#sessionTransacted is finally mapped to ConnectionFactoryUtils.JmsResourceSynchronization#transacted and default=false prevents commit being called at the end of transaction (JmsResourceSynchronization "thinks" that it does not participate transaction)

我对文档的理解正确吗,这里确实有错误吗?

Do I understand documentation right and there is really bug here?

推荐答案

在M. Deinum的指导下,我进行了更多实验,似乎我误解了 Spring管理的交易

Guided by M. Deinum, I made more experiments and it seems I wrongly understood term Spring-managed transaction

我只是认为Spring托管事务是由platformTransactionManager启动的.但是:

I simply thought that Spring managed transaction is started by platformTransactionManager. But:

  1. 如果platformTransactionManagerJtaTransactionManager且事务已启动,则它是 IS Spring管理的事务; JMS模板属性 sessionTransacted 被忽略,JMS模板是事务的一部分
  2. 如果platformTransactionManagerDataSourceTransactionManagerJpaTransactionManager,则
    • 如果sessionTransacted为false,则表明JMS模板不在事务中
    • 如果sessionTransacted为true,则JMS模板与事务同步:在JDBC/JPA事务上进行回调/回滚之后,在JMS事务上调用相应的提交/回滚
  1. If platformTransactionManager is JtaTransactionManager and transaction is started, it IS Spring managed transaction; JMS template attribute sessionTransacted is ignored and JMS template is part of transaction
  2. if platformTransactionManager is DataSourceTransactionManager or JpaTransactionManager then
    • if sessionTransacted is false, JMS template is not in transaction
    • if sessionTransacted is true, JMS template is synchronized with transaction: after callback/rollback on JDBC/JPA transaction correspondent commit/rollback is called on JMS transaction

这篇关于JMSTemplate中的setSessionTransacted到底是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆