Skip to content

Lazy Session initialisation for JmsTransactionManager #22468

Closed
@maciejmiklas

Description

@maciejmiklas

We have two transaction managers in our application: JPA and JMS. In order to just use @Transactional on every possible method we are using ChainedTransactionManager:

@Bean
@Primary
public PlatformTransactionManager transactionManager() {
  return new ChainedTransactionManager(jmsTransactionManager, jpaTransactionManager);
}

This is working as expected, however there is a one problem, that could be solved on Spring side. Every interaction with our application requires DB connection, but only few JMS. Because we've used such simplified transaction configuration, Spring starts both transactions every time.

JmsTransactionManager#doBegin creates JMS Session right on the beginning of each transaction. We would prefer to postpone creation of JMS Session until first message has to be send. This could be configurable.

Here is my implementation: https://gist.github.com/maciejmiklas/d5f61703e045792e6a54819c749f6252

It's based on original JmsTransactionManager with two modifications:

  •  JmsTransactionManager#doBegin does not start JMS Session
  • JmsResourceHolder has been overwritten, and method getSession() starts now JMS Session

I did not find a better way to implement it, so there is no pull request, but it gives an idea how it could work.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions