1、在启动类添加 @EnableTransactionManagement 注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@EnableTransactionManagement //开启事务支持
public class AdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(AdminServerApplication.class, args);
}
}
2、访问数据库的Service方法上添加注解 @Transactional
//多数据源需要配置指定事务管理器 @Transactional( transactionManager ="gxfullviewManager", rollbackFor=Exception.class) @Transactional(rollbackFor=Exception.class) public void addzcrelationByExcel(String workno, List
3、多数据源配置事务管理器(具体看自己的业务是否为 多数据源)
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
public class ClicSpringBootDataSourceConfig {
// @Primary
@Bean(value = "gxfullview")
// @ConditionalOnProperty(prefix = "beetlsql.ds.localapp")
@ConfigurationProperties("spring.datasource.druid.gxfullview")
DataSource dataSourceOne(){
return DruidDataSourceBuilder.create().build();
}
@Bean(value = "gxfullviewpolar")
// @ConditionalOnProperty(prefix = "beetlsql.ds.localapp")
@ConfigurationProperties("spring.datasource.druid.gxfullviewpolar")
DataSource dataSourceTwo(){
return DruidDataSourceBuilder.create().build();
}
@Bean(name = "gxfullviewManager")
public DataSourceTransactionManager gxfullviewManager(@Qualifier("gxfullview") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "gxfullviewpolarManager")
public DataSourceTransactionManager gxfullviewpolarManager(@Qualifier("gxfullviewpolar") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
}



