Commit cab46ca7 by gechengyang

提交多数据源runtime切换初版

parent af8e55c1
......@@ -155,6 +155,11 @@
<scope>system</scope>
<systemPath>${pom.basedir}/lib/ojdbc6-12.1.0.2.jar</systemPath>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.23</version>
</dependency>
</dependencies>
......
package com.brilliance.mda.runtime.mda.config;
import com.brilliance.mda.runtime.mda.util.DataSourceUtil;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
/**
* 动态数据源类
*/
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DataSourceUtil.getDB();
}
}
package com.brilliance.mda.runtime.mda.util;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
/**
* 数据源切换工具
*/
public class DataSourceUtil {
/**
* 默认数据源
*/
//TODO 这里默认数据源需要动态生成
public static final String DEFAULT_DS = "eibs";
/**
* 数据源属于一个公共的资源
* 采用可以保证在多线程情况下线程隔离
*/
private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();
/**
* 设置数据源名
* @param dbType
*/
public static void setDB(String dbType) {
contextHolder.set(dbType);
}
/**
* 获取数据源名
* @return
*/
public static String getDB() {
String dbName= (contextHolder.get()) ;
if(MdaUtils.isEmpty(dbName))
return DEFAULT_DS;
return dbName;
}
/**
* 清除数据源名
*/
public static void clearDB() {
contextHolder.remove();
}
}
\ No newline at end of file
......@@ -49,7 +49,7 @@ import static com.brilliance.mda.support.mybatis.MybatisArgumentAdapter.HOLDER_S
*
* @author hulei
*/
@Component
//@Component
public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession {
private static Logger log = LoggerFactory.getLogger(MyBatisDaoSession.class);
......@@ -273,6 +273,11 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession
sqlSessionTemplate.getSqlSessionFactory().openSession(false);
}
@Override
public void dbConnect(String s) {
}
@Override
public void dbDisconnect() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment