gpt4 book ai didi

java - spring boot xa事务数据源和jms

转载 作者:行者123 更新时间:2023-11-29 04:32:11 28 4
gpt4 key购买 nike

我用 spring-boot-starter-data-jpa 和 spring-boot-starter-activemq 做了一个 POC。我想在提交 jpa 事务时将 jms 消息推送到代理 (activeMQ)。

我的代码:UtilsateurService 具有“主要”事务:

@Service
public class UtilisateurService {

@Autowired
private UtilisateurRepository utilisateurRepository;

@Autowired
private SendMessage sendMessage;

@Transactional(rollbackOn = java.lang.Exception.class)
public Utilisateur create(Utilisateur utilisateur) throws Exception {
final Utilisateur result = utilisateurRepository.save(utilisateur);
sendMessage.send("creation utilisateur : " + result.getId());
throw new Exception("rollback");
//return result;
}
}

SendMessage 类女巫“管理”Jms 消息:

@Component
public class SendMessage {

@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;

@Value("${jms.queue.destination}")
private String destinationQueue;

public void send(String msg) {
this.jmsMessagingTemplate.convertAndSend(destinationQueue, msg);
}

}

我的主课:

@SpringBootApplication
@EnableJms
@EnableTransactionManagement
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

在抛出异常之前,JMS 消息被推送到 activeMq 代理上。所以我没有对经纪人进行“回滚”。

如何配置以运行 xa 事务?

最佳答案

您的 jmsTemplate 是否已处理?

jmsTemplate.setSessionTransacted(true); 

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/jms/support/JmsAccessor.html#setSessionTransacted-boolean-

public void setSessionTransacted(boolean sessionTransacted)

Set the transaction mode that is used when creating a JMS Session. Default is "false". Note that within a JTA transaction, the parameters passed to create(Queue/Topic)Session(boolean transacted, int acknowledgeMode) method are not taken into account. Depending on the Java EE transaction context, the container makes its own decisions on these values. Analogously, these parameters are not taken into account within a locally managed transaction either, since the accessor operates on an existing JMS Session in this case.

Setting this flag to "true" will use a short local JMS transaction when running outside of a managed transaction, and a synchronized local JMS transaction in case of a managed transaction (other than an XA transaction) being present. This has the effect of a local JMS transaction being managed alongside the main transaction (which might be a native JDBC transaction), with the JMS transaction committing right after the main transaction.

http://www.javaworld.com/article/2077963/open-source-tools/distributed-transactions-in-spring--with-and-without-xa.html

30.2.5 Transaction management

Spring provides a JmsTransactionManager that manages transactions for a single JMS ConnectionFactory. This allows JMS applications to leverage the managed transaction features of Spring as described in Chapter 17, Transaction Management. The JmsTransactionManager performs local resource transactions, binding a JMS Connection/Session pair from the specified ConnectionFactory to the thread. JmsTemplate automatically detects such transactional resources and operates on them accordingly.

In a Java EE environment, the ConnectionFactory will pool Connections and Sessions, so those resources are efficiently reused across transactions. In a standalone environment, using Spring’s SingleConnectionFactory will result in a shared JMS Connection, with each transaction having its own independent Session. Alternatively, consider the use of a provider-specific pooling adapter such as ActiveMQ’s PooledConnectionFactory class.

JmsTemplate can also be used with the JtaTransactionManager and an XA-capable JMS ConnectionFactory for performing distributed transactions. Note that this requires the use of a JTA transaction manager as well as a properly XA-configured ConnectionFactory! (Check your Java EE server’s / JMS provider’s documentation.)

Reusing code across a managed and unmanaged transactional environment can be confusing when using the JMS API to create a Session from a Connection. This is because the JMS API has only one factory method to create a Session and it requires values for the transaction and acknowledgment modes. In a managed environment, setting these values is the responsibility of the environment’s transactional infrastructure, so these values are ignored by the vendor’s wrapper to the JMS Connection. When using the JmsTemplate in an unmanaged environment you can specify these values through the use of the properties sessionTransacted and sessionAcknowledgeMode. When using a PlatformTransactionManager with JmsTemplate, the template will always be given a transactional JMS Session.

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/jms.html#jms-tx

关于java - spring boot xa事务数据源和jms,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43392774/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com