0

I am trying to upgrade a legacy spring-hibernate (no jpa) project to spring-jpa.

The project has some monkeying around datasource, transactionManager generation etc.., basically we are generating all of them (multiple datasources, multiple transactionManagers) programmatically via parsing the properties on our own.

Generating all these and having a map of different datasources and transactionManagers are ok. But I am unable to replace the TransactionInterceptor bean in context, so that I can utilize my pre-generated map to use different managers. When I try to generate a bean with name TransactionInterceptor, spring does not like it and context failed to come up.

So my question is if it's possible to replace the TransactionInterceptor or something else (like advisors etc..) to intercept and use proper tm.

spring boot : 3.3.3 spring-tx : 6.1.12

asked Feb 9, 2025 at 13:30

1 Answer 1

0

Please see Programmatic Transaction Management.

I’m not sure if this is the best approach for your case, but you could replace the TransactionInterceptor with a custom Advisor bean.

Tested on my computer.

import org.springframework.aop.Advisor;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionInterceptor;
@Configuration
public class TransactionInterceptorConfig {
 @Bean
 public Advisor customTransactionAdvisor(TransactionManager transactionManager) {
 var transactionInterceptor = new TransactionInterceptor(); // replace with your interceptor
 transactionInterceptor.setTransactionManager(transactionManager);
 // pointcut for classes/methods annotated with @Transactional
 var annotationMatchingPointcut =
 new AnnotationMatchingPointcut(Transactional.class, Transactional.class);
 return new DefaultPointcutAdvisor(annotationMatchingPointcut, transactionInterceptor);
 }
}
answered Feb 9, 2025 at 14:23
Sign up to request clarification or add additional context in comments.

1 Comment

I did something kinda hacky "for now" till I try your solution not setting this to "answer". thx for the help.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.