Skip to content

JMS transactions seem to need new contexts each time #189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ibmmqmet opened this issue Feb 17, 2025 · 1 comment
Open

JMS transactions seem to need new contexts each time #189

ibmmqmet opened this issue Feb 17, 2025 · 1 comment

Comments

@ibmmqmet
Copy link

ibmmqmet commented Feb 17, 2025

This is a simple program using two JMS connections. I'm running it inside Spring Boot, but some of the autowiring is a little bit more explicit to deal with the multiple ConnectionFactories.

UserTransaction utx = com.arjuna.ats.jta.UserTransaction.userTransaction();

XAConnectionFactory xacf2 = context.getBean("qm2", XAConnectionFactory.class);
XAConnectionFactory xacf3 = context.getBean("qm3", XAConnectionFactory.class);
ConnectionFactoryProxy cfp2 = new ConnectionFactoryProxy((XAConnectionFactory) xacf2, new TransactionHelperImpl(tm));
ConnectionFactoryProxy cfp3 = new ConnectionFactoryProxy((XAConnectionFactory) xacf3, new TransactionHelperImpl(tm);

// Should be able to set these up before doing any transactional work.
ctx2 = cfp2.createContext();
ctx3 = cfp3.createContext();
consumer = ctx2.createConsumer(q);
producer = ctx3.createProducer();

for (int i=0;i<2;i++) {
  utx.begin();

... do some work

  if (i==0)  // Alternate between commit/rollback outcomes
    utx.commit();
  else
    utx.rollback():
}

But it does not seem to work - the consumer/producer's operations are not all done inside a coordinated syncpoint. Variations on the code also failed in different ways (such as the two contexts running with independent, non-coordinated transactions.)

The only way I could get this to work was to move the creation of the contexts and the corresponding consumer/producer BELOW the utx.begin() line. So there's a new set of contexts created for each loop or transaction - the TM does not appear to reuse an existing JMSContext. This is very inefficient as creation of a JMSContext can be expensive.

The equivalent program using Atomikos as the TM does appear to work as expected.

Any thoughts?

@graben
Copy link
Contributor

graben commented Feb 18, 2025

ConnectionFactoryProxy isn't pooled, that's why creation of multiple JMSContexts is expected. Do use the pooled variant using messaginghub dependency instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants