Commit 94b9ef42 by s_guodong

还原platform,修改运行时支持多数据源切换

parent 99297dde
......@@ -6,7 +6,6 @@ import com.brilliance.mda.runtime.mda.driver.MdaEnv;
import com.brilliance.mda.runtime.mda.impl.Argument;
import com.brilliance.mda.runtime.mda.impl.AttributeValue;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.mda.support.mybatis.DynamicDataSourceContextHolder;
import com.ceb.gjjs.mda.bo.Bnssta;
import com.ceb.gjjs.mda.bo.Fipsta;
import org.slf4j.Logger;
......@@ -1585,9 +1584,6 @@ public class Platform {
//取得上下文
IContext ctx = MdaEnv.getContext();
ctx.setErrorCode(NO_ERROR);
String tableName = MdaUtils.getTableNameFromSQL(sql);
String tableDB = Platform.getTableDB(tableName);
DynamicDataSourceContextHolder.setDataSourceType(tableDB);
ctx.getDaoSession().dbExecuteSQL(sql);
}
......@@ -1774,9 +1770,6 @@ public class Platform {
//取得上下文
IContext ctx = MdaEnv.getContext();
ctx.setErrorCode(NO_ERROR);
// 切换数据源
String moduleDB = Platform.getModuleDB(module);
DynamicDataSourceContextHolder.setDataSourceType(moduleDB);
ctx.getDaoSession().dbRead(module, arguments);
}
......@@ -1804,7 +1797,7 @@ public class Platform {
ctx.storeData("DbMap", mapObj);
}
Map<String, String> map = (Map<String, String>) mapObj;
String listClass = list.getDataClass().toString().substring(6);
String listClass = list.getClass().toString().substring(6);
String moduleClass = listClass.replace(".Elst", ".");
String db = map.get(moduleClass);
log.trace("{}", "getModuleListDB " + moduleClass + " " + db);
......@@ -1877,9 +1870,6 @@ public class Platform {
//取得上下文
IContext ctx = MdaEnv.getContext();
ctx.setErrorCode(NO_ERROR);
// 切换数据源
String db = Platform.getModuleListsDB(lists);
DynamicDataSourceContextHolder.setDataSourceType(db);
ctx.getDaoSession().dbReadset(lists, maxSize, whereClause, datas);
}
......@@ -2011,9 +2001,6 @@ public class Platform {
//取得上下文
IContext ctx = MdaEnv.getContext();
ctx.setErrorCode(NO_ERROR);
String tableName = MdaUtils.getTableNameFromSQL(sql);
String tableDB = Platform.getTableDB(tableName);
DynamicDataSourceContextHolder.setDataSourceType(tableDB);
ctx.getDaoSession().dbExecuteSQL(sql, value);
}
......@@ -2670,7 +2657,7 @@ public class Platform {
//取得上下文
IContext ctx = MdaEnv.getContext();
ctx.setErrorCode(NO_ERROR);
String tbl = "com.ceb.gjjs.mda.bo." + tableName.substring(0, 1).toUpperCase() + tableName.substring(1);
String tbl = "com.brilliance.eibs.etrade." + tableName.substring(0, 1).toUpperCase() + tableName.substring(1);
Object mapObj = getStoredData("DbMap");
if (mapObj == null) {
String path = MdaUtils.catPath(ctx.getEnvConfig().getRootPath(), "WEB-INF/classes");
......@@ -3782,5 +3769,4 @@ public class Platform {
Platform.mdbDelete(mod);
}
}
\ No newline at end of file
......@@ -5,6 +5,7 @@ import com.brilliance.mda.runtime.mda.driver.MdaDriver;
import com.brilliance.mda.runtime.mda.driver.MdaEnv;
import com.brilliance.mda.runtime.mda.impl.AbstractDaoSession;
import com.brilliance.mda.runtime.mda.impl.Argument;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.mda.support.mybatis.count.CounterService;
import com.brilliance.mda.support.mybatis.dync.mapper.DbExecuteMapper;
import org.mybatis.spring.SqlSessionTemplate;
......@@ -96,30 +97,40 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession
@Override
public <T extends IModule> int dbInsert(T module) {
String moduleDB = MdaUtils.getModuleDB(module);
DynamicDataSourceContextHolder.setDataSourceType(moduleDB);
String sqlId = getSqlId(module.getClass(), SQLID_INSERT);
return executeUpdate(sqlId, module);
}
@Override
public <T extends IModule> int dbUpdate(T module) {
String moduleDB = MdaUtils.getModuleDB(module);
DynamicDataSourceContextHolder.setDataSourceType(moduleDB);
String sqlId = getSqlId(module.getClass(), SQLID_UPDATE);
return executeUpdate(sqlId, module);
}
@Override
public <T extends IModule> int dbDelete(T module) {
String moduleDB = MdaUtils.getModuleDB(module);
DynamicDataSourceContextHolder.setDataSourceType(moduleDB);
String sqlId = getSqlId(module.getClass(), SQLID_DELETE);
return executeUpdate(sqlId, module);
}
@Override
public <T extends IModule> int dbDelete(T m, Argument... args) {
String moduleDB = MdaUtils.getModuleDB(m);
DynamicDataSourceContextHolder.setDataSourceType(moduleDB);
Class<T> clazz = (Class<T>) m.getClass();
MybatisArgumentAdapter adapter = new MybatisArgumentAdapter(DELETE, clazz, args);
return executeUpdate(getSqlId(clazz, SQLID_DYNCDEL), adapter.getSqlParams());
}
public <T extends IModule> int dbDelete(T module, Serializable... id) {
String moduleDB = MdaUtils.getModuleDB(module);
DynamicDataSourceContextHolder.setDataSourceType(moduleDB);
Class<T> clazz = (Class<T>) module.getClass();
String sqlId = getSqlId(clazz, SQLID_DELBYIDS);
return executeUpdate(sqlId, Arrays.asList(id));
......@@ -136,6 +147,8 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession
@Override
public <T extends IModule> T dbRead(T module, Argument... args) {
String moduleDB = MdaUtils.getModuleDB(module);
DynamicDataSourceContextHolder.setDataSourceType(moduleDB);
MybatisArgumentAdapter adapter = new MybatisArgumentAdapter(SELECT, module.getClass(), args);
T entity = this.dyncReadOne(module.getClass(), adapter);
......@@ -147,6 +160,9 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession
@Override
public <T extends IModule> int dbReadset(IModuleList<T> list, int limitSize, Argument... args) {
String moduleDB = MdaUtils.getModuleListDB(list);
DynamicDataSourceContextHolder.setDataSourceType(moduleDB);
list.clear();
MybatisArgumentAdapter adapter = new MybatisArgumentAdapter(SELECT, list.getDataClass(), args);
String temp = adapter.getSqlTemplate();
......@@ -166,6 +182,9 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession
@Override
public <T extends IModule> int dbReadset(IModuleList<T> list, Argument... args) {
String moduleDB = MdaUtils.getModuleListDB(list);
DynamicDataSourceContextHolder.setDataSourceType(moduleDB);
list.clear();
this.dbReadset(list, 0, args);
return list.size();
......@@ -207,6 +226,8 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession
@Override
public <T extends IModule> int dbReadset(IModuleList<T> list, String whereSql, Object[] objects) {
String moduleDB = MdaUtils.getModuleListDB(list);
DynamicDataSourceContextHolder.setDataSourceType(moduleDB);
list.clear();
MybatisArgumentAdapter adapter = new MybatisArgumentAdapter(SELECT, list.getDataClass(), whereSql, objects);
......@@ -218,6 +239,9 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession
@Override
public <T extends IModule> int dbReadset(IModuleList<T>[] lists, int maxSize, String whereClause, Object[] datas) {
String moduleDB = MdaUtils.getModuleListsDB(lists);
DynamicDataSourceContextHolder.setDataSourceType(moduleDB);
List<Class<? extends IModule>> clazzList = new ArrayList<>();
for (IModuleList iModule : lists) {
clazzList.add(iModule.getDataClass());
......@@ -237,6 +261,10 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession
@Override
public void dbExecuteSQL(String sql, Object... objects) {
String tableName = MdaUtils.getTableNameFromSQL(sql);
String tableDB = MdaUtils.getTableDB(tableName);
DynamicDataSourceContextHolder.setDataSourceType(tableDB);
Map<String, Object> map = new HashMap<>();
if (objects != null) {
sql = MybatisArgumentAdapter.convertPlaceHolder(sql, objects, map);
......
......@@ -35,6 +35,8 @@ import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.Semaphore;
import static com.brilliance.mda.runtime.mda.Constants.NO_ERROR;
public class MdaUtils {
private static final Logger log = LoggerFactory.getLogger(MdaUtils.class);
......@@ -1345,4 +1347,185 @@ public class MdaUtils {
}
/**
* source:sysmod.@0138.script
*/
public static String getModuleDB(IModule module) {
//取得上下文
IContext ctx = MdaEnv.getContext();
ctx.setErrorCode(NO_ERROR);
Object mapObj = getStoredData("DbMap");
if (mapObj == null) {
String path = MdaUtils.catPath(ctx.getEnvConfig().getRootPath(), "WEB-INF/classes");
mapObj = MdaUtils.invoke("com.brilliance.xml.DbMapping", "getMap", path);
ctx.storeData("DbMap", mapObj);
}
Map<String, String> map = (Map<String, String>) mapObj;
String moduleClass = module.getClass().toString().substring(6);
String db = map.get(moduleClass);
if (db == null || db.contains(",")) {
db = getDefaultDB();
}
log.trace("{}", "getModuleDB " + module.getName() + " " + db);
return db;
}
public static Object getStoredData(String key) {
//取得上下文
IContext ctx = MdaEnv.getContext();
ctx.setErrorCode(NO_ERROR);
Object value = ctx.restoreData(key);
if (value != null)
ctx.storeData(key, value);
return value;
}
/**
* source:sysmod.@0157.script
*/
public static String getDefaultDB() {
//取得上下文
IContext ctx = MdaEnv.getContext();
ctx.setErrorCode(NO_ERROR);
String db = (String) getStoredData("DefaultDB." + getModuleRoot());
if (db == null) {
//从前手交易获取默认数据库
String caller = (String) ctx.restoreData(ctx.getTransName() + ".caller");
if (caller != null) {
String lastDB = (String) getStoredData("LastDB." + caller);
log.debug("{}", "caller LastDB=" + lastDB);
setLastDB(lastDB);
db = (String) getStoredData("DefaultDB." + caller);
log.debug("{}", "caller DefaultDB=" + db);
setDefaultDB(db);
} else {
String fileName = MdaUtils.catPath(ctx.getEnvConfig().getRootPath(), "cfg/transdb.properties");
String trnnam = ctx.getTransName();
db = MdaUtils.getPropertyValue(fileName, trnnam);
if (!MdaUtils.isEmpty(db))
log.debug("{}", trnnam + " db = " + db);
if (MdaUtils.isEmpty(db) && trnnam.length() > 6) {
db = MdaUtils.getPropertyValue(fileName, trnnam.substring(0, 6));
if (!MdaUtils.isEmpty(db))
log.debug("{}", trnnam.substring(0, 6) + " db =" + db);
}
if (MdaUtils.isEmpty(db)) {
db = MdaUtils.getPropertyValue(fileName, trnnam.substring(0, 3) + "***");
if (!MdaUtils.isEmpty(db))
log.debug("{}", trnnam.substring(0, 3) + "***" + " db =" + db);
}
if (MdaUtils.isEmpty(db)) {
db = MdaUtils.getPropertyValue(fileName, trnnam.substring(0, 2) + "****");
if (!MdaUtils.isEmpty(db))
log.debug("{}", trnnam.substring(0, 2) + "****" + " db =" + db);
}
if (MdaUtils.isEmpty(db)) {
db = MdaUtils.getPropertyValue(fileName, "***" + trnnam.substring(3));
if (!MdaUtils.isEmpty(db))
log.debug("{}", "***" + trnnam.substring(3) + " db =" + db);
}
if (MdaUtils.isEmpty(db)) {
db = MdaUtils.getPropertyValue(fileName, trnnam.substring(0, 2) + "*" + trnnam.substring(3));
if (!MdaUtils.isEmpty(db))
log.debug("{}", trnnam.substring(0, 2) + "*" + trnnam.substring(3) + " db =" + db);
}
if (!MdaUtils.isEmpty(db)) {
log.debug("{}", "transdb.properties DefaultDB=" + db);
setDefaultDB(db);
} else {
db = "eibs";
}
}
}
return db;
}
public synchronized static IModuleRoot getModuleRoot() {
//取得上下文
IContext ctx = MdaEnv.getContext();
ctx.setErrorCode(NO_ERROR);
return (IModuleRoot) MdaDriver.getModule(null, "");
}
public static void setDefaultDB(String db) {
//取得上下文
IContext ctx = MdaEnv.getContext();
ctx.setErrorCode(NO_ERROR);
if (db != null) {
ctx.storeData("DefaultDB." + getModuleRoot(), db);
log.debug("{}", "DefaultDB change to " + db);
}
}
public static void setLastDB(String db) {
//取得上下文
IContext ctx = MdaEnv.getContext();
ctx.setErrorCode(NO_ERROR);
if (db != null) {
ctx.storeData("LastDB." + getModuleRoot(), db);
}
}
public static String getModuleListDB(IModuleList list) {
//取得上下文
IContext ctx = MdaEnv.getContext();
ctx.setErrorCode(NO_ERROR);
Object mapObj = getStoredData("DbMap");
if (mapObj == null) {
String path = MdaUtils.catPath(ctx.getEnvConfig().getRootPath(), "WEB-INF/classes");
mapObj = MdaUtils.invoke("com.brilliance.xml.DbMapping", "getMap", path);
ctx.storeData("DbMap", mapObj);
}
Map<String, String> map = (Map<String, String>) mapObj;
String listClass = list.getDataClass().toString().substring(6);
String moduleClass = listClass.replace(".Elst", ".");
String db = map.get(moduleClass);
log.trace("{}", "getModuleListDB " + moduleClass + " " + db);
if (db == null || db.contains(",")) {
db = getDefaultDB();
}
return db;
}
public static String getModuleListsDB(IModuleList[] lists) {
//取得上下文
IContext ctx = MdaEnv.getContext();
ctx.setErrorCode(NO_ERROR);
String db = null;
for (IModuleList list : lists) {
db = getModuleListDB(list);
if (!"eibs".equals(db))
return db;
}
return db;
}
public static String getTableDB(String tableName) {
//取得上下文
IContext ctx = MdaEnv.getContext();
ctx.setErrorCode(NO_ERROR);
String tbl = "com.ceb.gjjs.mda.bo." + tableName.substring(0, 1).toUpperCase() + tableName.substring(1);
Object mapObj = getStoredData("DbMap");
if (mapObj == null) {
String path = MdaUtils.catPath(ctx.getEnvConfig().getRootPath(), "WEB-INF/classes");
mapObj = MdaUtils.invoke("com.brilliance.xml.DbMapping", "getMap", path);
ctx.storeData("DbMap", mapObj);
}
Map<String, String> map = (Map<String, String>) mapObj;
String db = map.get(tbl);
if (db == null || db.contains(",")) {
db = getDefaultDB();
}
log.trace("{}", "getTableDB " + tableName + " " + db);
return db;
}
}
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