Spring Hibernate and JMS transaction synchronization

I am working on a standalone application that uses both JMS and Hibernate.

The documentation suggests using JTA if I want to have transactions across both resources.

However, right now, using the annotated DAO @Transaction method (and HibernateTransactionManager), this already works. When I call send () on the JmsTemplate, the message is not sent immediately, but rather the JMS session is passed with the Hibernate session as the method returns.

I did not know how this is possible without the JtaTransactionManager, so I checked the source code. It turns out that both wrappers for Hibernate and JmsTemplate register sessions with the TransactionSynchronizationManager, and the JMS session will be executed when the Hibernate session is running.

What is the difference between this transaction and JTA. Can I use this to replace the latter?

+5
source share
2 answers

In short, you cannot get two-phase commit support without JTATransactionManager and XA data sources.

What you are observing is the coordination of two local transactions that support only single-phase commit. Roughly performing this sequence of events ...

  • Run JMS Transaction
  • Reading JMS Message
  • Run JDBC Transaction
  • Write to database
  • Commit JDBC Transaction
  • Commit / Acknowledge JMS

JMS, JDBC, JMS , Hibernate/JDBC . - JMS acknowledge="auto" Hibernate .

, , , - Hibernate , , JMS-. , JMS , .

  • MessageListener

  • , - , . " ", MessageListener

JMS- XA (global), .

JMS- XA, JDBC , JTATransactionManager LastResourceCommitOptimisation. JTATransactionManagers , JOTM

JavaWorld .

+12

, : -

, JtaTransactionManager

spring: - JTA , Spring s JtaTransactionManager

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-jta.html

0

All Articles