SpringBoot多数据库事务管理

0

如果SpringBoot同时加入多个数据源,不能正确获取事务管理器,可以使用下面方法处理:

/**
 * 配置事务
 * 
 * 配置JDBC事务事务为默认的事务,Neo4j事务需要手动指定`@Transactional(transactionManager = "neo4jTransactionManager")`。
 * 
 * @author acgist
 */
@Configuration(proxyBeanMethods = false)
@Import({DataSourceAutoConfiguration.class, MybatisPlusAutoConfiguration.class})
//@Import({DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, MybatisPlusAutoConfiguration.class})
public class TransactionConfiguration {
	
	@Bean("transactionManager")
	@Primary
	public DataSourceTransactionManager transactionManager(
		Environment environment,
		DataSource dataSource,
		ObjectProvider<TransactionManagerCustomizers> transactionManagerCustomizers
	) {
		final DataSourceTransactionManager transactionManager = this.createTransactionManager(environment, dataSource);
		transactionManagerCustomizers.ifAvailable((customizers) -> customizers.customize(transactionManager));
		return transactionManager;
	}
	
	private DataSourceTransactionManager createTransactionManager(Environment environment, DataSource dataSource) {
		return
			environment.getProperty("spring.dao.exceptiontranslation.enable", Boolean.class, Boolean.TRUE)
			?
			new JdbcTransactionManager(dataSource)
			:
			new DataSourceTransactionManager(dataSource);
	}
	
	@Bean("neo4jTransactionManager")
	public Neo4jTransactionManager transactionManager(
			Driver driver,
			DatabaseSelectionProvider databaseNameProvider,
			ObjectProvider<TransactionManagerCustomizers> optionalCustomizers
	) {
		final Neo4jTransactionManager transactionManager = new Neo4jTransactionManager(driver, databaseNameProvider);
		optionalCustomizers.ifAvailable((customizer) -> customizer.customize(transactionManager));
		return transactionManager;
	}
	
}

默认使用JDBC事务管理器,如果使用Neo4j,可以指定事务管理器@Transactional(transactionManager = "neo4jTransactionManager")