Public CMS集成多个数据源很简单,只需要按照以下几步操作:
1. 修改ApplicationConfig.dataSource方法增加other
@Bean
public DataSource dataSource() throws PropertyVetoException {
CmsDataSource bean = new CmsDataSource(getDirPath(CommonConstants.BLANK) + CmsDataSource.DATABASE_CONFIG_FILENAME);
try {
bean.put("other", CmsDataSource
.initDataSource(CmsDataSource.loadDatabaseConfig(getDirPath(CommonConstants.BLANK) + "other.properties")));
} catch (IOException e) {
}
CmsDataSource.initDefautlDataSource();
return bean;
}2. 在操作数据库,并开启事务前切换数据源
CmsDataSource.setDataSourceName("other");3. 在访问数据库完毕应该还原数据源
CmsDataSource.resetDataSourceName();
具体原理就是继承并实现了框架的数据源路由类,使用线程变量来存储当前数据源名称,还原数据源也是把线程变量还原,防止后续其他任务读取到被污染的线程变量
标签:PublicCMS
0条评论