Scritto da
  • email
  • twitter
  • linkedin
  • linkedin

Spring's declarative transaction management on steroids

As you probably already know Spring Boot and Java based configuration have almost completely replaced the old way of configuring and building Spring-based applications. Following up the post on Spring dynamic transaction management our goal is to show a way to make Spring Boot dynamically select the proper transaction manager for a specific transaction.

The game is simple :) We only need to replicate the old XML configuration in Java, fitting with some little changes in Spring's way of defining a DomainTransactionInterceptorInjector. Thus, first of all, let's see how to define The interceptor injector:

    String[] names = beanFactory.getBeanNamesForType(TransactionInterceptor.class);
        for (String name : names) {
            BeanDefinition bd = beanFactory.getBeanDefinition(name);

We only just need to scan all Spring Beans and find the TransactionInterceptor one; then we need to "hack" its definition with our custom TransactionInterceptor. Here you can find how Syncope does the magic to manage multi-tenancy with its custom DomainTransactionInterceptor. Finally we just have to add our custom DomainTransactionInterceptorInjector to Spring Java-based configuration, like this:

    public static BeanFactoryPostProcessor domainTransactionInterceptorInjector() {
        return new DomainTransactionInterceptorInjector();

I shared on github a simple project of mine that shows how to migrate from Syncope 2 persistence layer Spring XML-based configuration to Spring Boot Java-based configuration. In the project you can also find a working example of dynamic transaction management in a Java-based fashion.

Enjoy ;)

2 VOTIYes, Excellent!Yes, Excellent!
Ti è stato utile questo articolo?
Dal Blog di Tirasa
Quello che facciamo, studiamo e pensiamo tutti i giorni lo condividiamo qui.
Vai al blog >