helloworlde / SpringBoot-DynamicDataSource

Dynamic datasource, multiple datasource of spring boot/ distribution transaction; spring boot 多数据源,动态数据源,分布式事务
Apache License 2.0
698 stars 314 forks source link

关于切换不成功问题 #17

Closed lishuanghe214 closed 4 years ago

lishuanghe214 commented 4 years ago

private final String[] QUERY_PREFIX = {"list"};

控制台打印 Switch DataSource to [slave] in Method [List com.weiyin.streetlamp.menu.dao.MenuDao.listAll()] Restore DataSource to [master] in Method [List com.weiyin.streetlamp.menu.dao.MenuDao.listAll()]

但是查询的还是主库中的数据,我配置的一主一次从

lishuanghe214 commented 4 years ago

我的没有xml @Bean //@ConfigurationProperties(prefix = "mybatis") public SqlSessionFactoryBean sqlSessionFactoryBean() { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); // Here to config mybatis // sqlSessionFactoryBean.setTypeAliasesPackage("cn.com.hellowood.dynamicdatasource.mapper"); // sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("mappers/**Mapper.xml")); // Here is very important, if don't config this, will can't switch datasource // put all datasource into SqlSessionFactoryBean, then will autoconfig SqlSessionFactory sqlSessionFactoryBean.setDataSource(dynamicDataSource()); return sqlSessionFactoryBean; }

lishuanghe214 commented 4 years ago

Switch DataSource to [slave] in Method [List com.weiyin.streetlamp.menu.dao.MenuDao.listAll()] 2020-11-03 12:36:43.669 [http-nio-0.0.0.0-8001-exec-7] INFO c.w.s.dynamicdatasource.DynamicRoutingDataSource - Current DataSource is [slave] 2020-11-03 12:36:43.674 [http-nio-0.0.0.0-8001-exec-7] INFO c.w.s.dynamicdatasource.DynamicDataSourceAspect - Restore DataSource to [master] in Method [List com.weiyin.streetlamp.menu.dao.MenuDao.listAll()] 控制台信息是这样,但是读不到数据

helloworlde commented 4 years ago

You can reference this document https://github.com/helloworlde/SpringBoot-DynamicDataSource/blob/master/Issues.md