多数据源,手动事务注解
获取容器中的bean
@Component public class ApplicationContextUtil implements ApplicationContextAware { public static ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { ApplicationContextUtil.applicationContext = applicationContext; } }
数据源配置
@Configuration @EnableTransactionManagement public class Gxzd37DataSourceConfig { /** * 数据源37 */ @Bean(name = "dataSource37") @ConfigurationProperties(prefix = "spring.37datasource.druid") public DruidDataSource dataSource() { return new DruidDataSource(); } @Bean(name = "jdbcTemplate37") public JdbcTemplate jdbcTemplate(@Qualifier("dataSource37") DataSource dataSource) { return new JdbcTemplate(dataSource); } @Bean(name = "namedParameterJdbcTemplate37") public NamedParameterJdbcTemplate namedParameterJdbcTemplate(@Qualifier("dataSource37") DataSource dataSource) { return new NamedParameterJdbcTemplate(dataSource); } @Bean("transactonManager37") public PlatformTransactionManager getTransactionManager(@Qualifier("dataSource37")DataSource dataSource){ return new DataSourceTransactionManager(dataSource); } } @Configuration @EnableTransactionManagement public class Gxzd47DataSourceConfig { /** * 数据源47 */ @Bean(name = "dataSource47") @ConfigurationProperties(prefix = "spring.gxzd47datasource.druid") public DruidDataSource dataSource() { return new DruidDataSource(); } @Bean(name = "jdbcTemplate47") public JdbcTemplate jdbcTemplate(@Qualifier("dataSource47") DataSource dataSource) { return new JdbcTemplate(dataSource); } @Bean(name = "namedParameterJdbcTemplate47") public NamedParameterJdbcTemplate namedParameterJdbcTemplate(@Qualifier("dataSource47") DataSource dataSource) { return new NamedParameterJdbcTemplate(dataSource); } @Bean("transactonManager47") public PlatformTransactionManager getTransactionManager(@Qualifier("dataSource47")DataSource dataSource){ return new DataSourceTransactionManager(dataSource); } }
事务注解
/** * @description: 共享字典手动事务注解 */ @Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) public @interface GxzdTranstionManager { String[] transactonManagers() default {}; }
切面类
@Component @Aspect @Order(-1) public class GxzdTranstionManagerAOP{ @Pointcut(value = "@annotation(cn.crl.zdgl.config.GxzdTranstionManager)") public void methodPointcut(){} @Around(value = "methodPointcut()") public Object transactionalAspectAround(ProceedingJoinPoint point) throws Throwable { MethodSignature signature = (MethodSignature)point.getSignature(); Method method = signature.getMethod(); GxzdTranstionManager annotation = method.getAnnotation(GxzdTranstionManager.class); String[] managers = annotation.transactonManagers(); // 放入栈中,保证先进后出,防止ransaction synchronization is not active Stack<PlatformTransactionManager> managerStack = new Stack<>(); Stack<TransactionStatus> statusStack = new Stack<>(); if(managers.length > 0){ for (String managerName : managers) { PlatformTransactionManager manager = (PlatformTransactionManager)ApplicationContextUtil.applicationContext.getBean(managerName); TransactionStatus transactionStatus = manager.getTransaction(new DefaultTransactionDefinition()); managerStack.push(manager); statusStack.push(transactionStatus); } } try { Object proceed = point.proceed(); while (!managerStack.isEmpty()){ managerStack.pop().commit(statusStack.pop()); } return proceed; } catch (Throwable throwable) { throwable.printStackTrace(); while (!managerStack.isEmpty()){ managerStack.pop().rollback(statusStack.pop()); } throw new Exception(throwable); } } }
业务类
@Service public class Service { @Autowired @Qualifier("jdbcTemplate37") private JdbcTemplate jdbcTemplate37; @Autowired @Qualifier("jdbcTemplate47") private JdbcTemplate jdbcTemplate47; @GxzdTranstionManager(transactonManagers = {"transactonManager37","transactonManager47"}) public Integer add(Object object){ new OtherDao(jdbcTemplate37).add(object)); new OtherDao(jdbcTemplate47).add(object)); return 1; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端