Transaction support for Spring Boot

Keywords: Programming Spring JDBC Database

1. Automatic Configuration of Transaction Manager

1 Description

When using JDBC as data access technology, Spring Boot defines a Bean for Platform Transaction Manager to implement DataSource Transaction Manager.

When using JPA as database access technology, Spring Boot defines a Bean for Platform Transaction Manager to implement JpaBase Configuration.

2 source code

//Source code for JDBC transaction configuration
@Configuration
@ConditionalOnClass({ JdbcTemplate.class, PlatformTransactionManager.class })
@AutoConfigureOrder(Ordered.LOWEST_PRECEDENCE)
public class DataSourceTransactionManagerAutoConfiguration {

    @Autowired(required = false)
    private DataSource dataSource;

    @Bean
    @ConditionalOnMissingBean(PlatformTransactionManager.class)
    @ConditionalOnBean(DataSource.class)
    public DataSourceTransactionManager transactionManager() {
        return new DataSourceTransactionManager(this.dataSource);
    }

    @ConditionalOnMissingBean(AbstractTransactionManagementConfiguration.class)
    @Configuration
    @EnableTransactionManagement
    protected static class TransactionManagementConfiguration {

    }

}
//Source Code for JPA Transaction Configuration
@EnableConfigurationProperties(JpaProperties.class)
@Import(DataSourceInitializedPublisher.Registrar.class)
public abstract class JpaBaseConfiguration implements BeanFactoryAware {

    private static final String[] NO_PACKAGES = new String[0];

    private ConfigurableListableBeanFactory beanFactory;

    @Autowired
    private DataSource dataSource;

    @Autowired(required = false)
    private PersistenceUnitManager persistenceUnitManager;

    @Autowired
    private JpaProperties jpaProperties;

    @Autowired(required = false)
    private JtaTransactionManager jtaTransactionManager;

    @Bean
    @ConditionalOnMissingBean(PlatformTransactionManager.class)
    public PlatformTransactionManager transactionManager() {
        return new JpaTransactionManager();
    }
    ......
}

2. Automatically open annotation transaction support

Spring Boot's class for configuring transactions is Transaction AutoConfiguration, which relies on JpaBase Configuration and DataSource Transaction Manager AutoConfiguration. The code is as follows:

@Configuration
@ConditionalOnClass({ TransactionTemplate.class, PlatformTransactionManager.class })
@ConditionalOnSingleCandidate(PlatformTransactionManager.class)
@AutoConfigureAfter({ JtaAutoConfiguration.class, JpaBaseConfiguration.class,
        DataSourceTransactionManagerAutoConfiguration.class })
public class TransactionAutoConfiguration {

    @Autowired
    private PlatformTransactionManager transactionManager;

    @Bean
    @ConditionalOnMissingBean
    public TransactionTemplate transactionTemplate() {
        return new TransactionTemplate(this.transactionManager);
    }

}

In the Data Source Transaction Manager AutoConfiguration configuration, declarative transaction support is also enabled. The code is as follows:

@Configuration
@ConditionalOnClass({ JdbcTemplate.class, PlatformTransactionManager.class })
@AutoConfigureOrder(Ordered.LOWEST_PRECEDENCE)
public class DataSourceTransactionManagerAutoConfiguration {

    @Autowired(required = false)
    private DataSource dataSource;

    @Bean
    @ConditionalOnMissingBean(PlatformTransactionManager.class)
    @ConditionalOnBean(DataSource.class)
    public DataSourceTransactionManager transactionManager() {
        return new DataSourceTransactionManager(this.dataSource);
    }

    @ConditionalOnMissingBean(AbstractTransactionManagementConfiguration.class)
    @Configuration
    @EnableTransactionManagement
    protected static class TransactionManagementConfiguration {

    }

}

So in Spring Boot, there is no need to display the open annotation using @Enable Transaction Management.

Posted by jdaura on Tue, 29 Jan 2019 03:39:16 -0800