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
1 Answer 1
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);
}
}
1 Comment
Explore related questions
See similar questions with these tags.