Spring+myBatis的动态多数据源 未能成功切换 本帖最后由 ilyzj 于 2013-01-30 20:45:20 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 /////////////////////////////////ACTION类/////////////////////////////////////////private CommentInfoLogic commentInfoLogic; private static Logger logger = Logger.getLogger(CommentInfoService.class); public String addCommentInfo(){ String devID = getRequest().getParameter(MsgCodeInfo.DEV_ID); ....... CommentInfo commentInfo = new CommentInfo(id ,appID, productID, rID, rName, uid, ip,userArea, comment, reserve); reInfo = commentInfoLogic.addCommentInfo(devID,commentInfo); return NONE; } public CommentInfoLogic getCommentInfoLogic() { return commentInfoLogic; } public void setCommentInfoLogic(CommentInfoLogic commentInfoLogic) { this.commentInfoLogic = commentInfoLogic; }///////////////////////////////LOGIC类///////////////////////////////////////////private CommentInfoDao commentInfoDao; private DevDao devDao; public String addCommentInfo(String devID, CommentInfo commentInfo) { Map<String, Object> params = new HashMap<String, Object>(); Dev dev = devDao.findByParams(params); if(dev != null && !StringHelper.Empty.equals(dev.getServerID())){ //变库 DynamicDataSource dynamicDataSource = (DynamicDataSource) ApplicationContextHelper.getBean("dynDataSource"); dynamicDataSource.selectDataSource(dev.getServerID()); } commentInfo = commentInfoDao.insert(commentInfo); return ""; } 两个属性的getter & settter方法//////////////////////////////数据库操作类/////////////////////////////////public class BaseDaoImpl<T, PK extends Serializable> extends SqlSessionDaoSupport implements BaseDao<T, PK> { @Autowired(required = true) @Resource(name = "sqlSessionFactory") public void setSuperSqlSessionFactory(SqlSessionFactory sqlSessionFactory) { super.setSqlSessionFactory(sqlSessionFactory); } // 保存 public T insert(T entity) { try { SqlSession session = this.getSqlSession(); session.insert(this.getClassSimpleName(entity) + ".save",entity); return entity; } catch (RuntimeException re) { logger.error("insert " + entity.getClass().getName() + " failed :{}",re); return null; } }}[/code]现在Dbinfo有如下数据:问题描述:我用以代码测试,发现结果是正确的,在两个数据库分别插入进去了一条CommentInfo记录. public static void main(String... args){ CommentInfoLogic logic = (CommentInfoLogic) ApplicationContextHelper.getBean("commentInfoLogic"); CommentInfo info = new CommentInfo(); info.setId(DateHelper.getUUID()); info.setCreateTime(new Date()); logic.addCommentInfo("1", info); logic.addCommentInfo("2", info); }但是,现在通过web请求,做如下测试.请求URL:http://localhost:8081/comment_service_webv3/service/CmmService/Add?参数串将logic代码稍做调整.public String addCommentInfo(String devID, CommentInfo commentInfo) { Map<String, Object> params = new HashMap<String, Object>(); Dev dev = devDao.findByParams(params); if(dev != null && !StringHelper.Empty.equals(dev.getServerID())){ //变库 DynamicDataSource dynamicDataSource = (DynamicDataSource) ApplicationContextHelper.getBean("dynDataSource"); //将数据库强制变为2库. dynamicDataSource.selectDataSource(2); } commentInfo = commentInfoDao.insert(commentInfo); return ""; }结果: 预期该条数据应该查入2库,但实际却仍然插入spring配置的默认datasource库(<property name="defaultTargetDataSource" ref="dataSource" />)里面.思考:这不科学呀,我两种方法基本没有区别呀.最后经过比对,找了大概出错的地方,是数据库层getSession出的不一样,但是不知道是为什么.第一种通过代码测试,debug截图如下图,发现其获得session中有动态添加进去的datasource.但是通过web请求过来的,然后获得session里面,就没有动态添加进去的datasource,所以只能操作默认配置的数据库了,如下图实在是想不通,两都方式对logic类来说,都是spring里面得到的对象,为什么结果会不一样呢.请大家指点,为什么?应当如何修改?谢谢. 给id="dynDataSource"的bean加上scope="prototype"看下。 加上之后,代码测试也不行了.public static void main(String... args){ CommentInfoLogic logic = (CommentInfoLogic) ApplicationContextHelper.getBean("commentInfoLogic"); CommentInfo info = new CommentInfo(); info.setId(DateHelper.getUUID()); info.setCreateTime(new Date()); logic.addCommentInfo("1", info); logic.addCommentInfo("2", info); }结果两条数据都是插入到了默认数据库了.这样就和web请求是一样的结果.就是获得的session里面没有动态添加进去的datasource了.这是那里的原因呢.好像差一点点,就能解决了,就是不能捅破这最后一层纸呀.郁闷哟. 已经看到了,就是getSession出来的那个SqlSeesion里面的_targetDataSources没有保存,我后来动态set进去的datasource. 看看这个http://jijun87120681.iteye.com/blog/1320799ApplicationContextHelper在XML中注册没 1、消息中间件,能解决2、 private Map<Object, Object> ,以前写的时候,第二个参数定义的是sessionfactory ,但当时的系统是统一的数据库 JAVA wemb客户端 CIM 搞过存储或高手的帮小弟看看 汽车租赁系统 急!!关于Sevlet不能接受request.getAttribute() Axis1.4容器 wrapped/literal 样式加密认证问题! 求一个算法 我刚刚有个JAVA题目请大家帮忙啊?,急的很,帮帮忙, 如何使第一行和第一列的数据固定 Java EE eclipse环境问题 在线求助`~有关JAVA~~SERVLET的 tomcat目录问题 求MYBATIS比较好的学习资料 关于hibernate二级缓存出现的问题,求教
private CommentInfoLogic commentInfoLogic;
private static Logger logger = Logger.getLogger(CommentInfoService.class);
public String addCommentInfo(){
String devID = getRequest().getParameter(MsgCodeInfo.DEV_ID);
.......
CommentInfo commentInfo = new CommentInfo(id ,appID, productID, rID, rName, uid, ip,userArea, comment, reserve);
reInfo = commentInfoLogic.addCommentInfo(devID,commentInfo);
return NONE;
}
public CommentInfoLogic getCommentInfoLogic() {
return commentInfoLogic;
}
public void setCommentInfoLogic(CommentInfoLogic commentInfoLogic) {
this.commentInfoLogic = commentInfoLogic;
}
///////////////////////////////LOGIC类///////////////////////////////////////////
private CommentInfoDao commentInfoDao;
private DevDao devDao; public String addCommentInfo(String devID, CommentInfo commentInfo) {
Map<String, Object> params = new HashMap<String, Object>();
Dev dev = devDao.findByParams(params);
if(dev != null && !StringHelper.Empty.equals(dev.getServerID())){
//变库
DynamicDataSource dynamicDataSource = (DynamicDataSource) ApplicationContextHelper.getBean("dynDataSource");
dynamicDataSource.selectDataSource(dev.getServerID());
}
commentInfo = commentInfoDao.insert(commentInfo);
return "";
}
两个属性的getter & settter方法//////////////////////////////数据库操作类/////////////////////////////////
public class BaseDaoImpl<T, PK extends Serializable> extends
SqlSessionDaoSupport implements BaseDao<T, PK> { @Autowired(required = true)
@Resource(name = "sqlSessionFactory")
public void setSuperSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
super.setSqlSessionFactory(sqlSessionFactory);
} // 保存
public T insert(T entity) {
try {
SqlSession session = this.getSqlSession();
session.insert(this.getClassSimpleName(entity) + ".save",entity);
return entity;
} catch (RuntimeException re) {
logger.error("insert " + entity.getClass().getName() + " failed :{}",re);
return null;
}
}
}
[/code]现在Dbinfo有如下数据:
问题描述:
我用以代码测试,发现结果是正确的,在两个数据库分别插入进去了一条CommentInfo记录.
public static void main(String... args){
CommentInfoLogic logic = (CommentInfoLogic) ApplicationContextHelper.getBean("commentInfoLogic");
CommentInfo info = new CommentInfo();
info.setId(DateHelper.getUUID());
info.setCreateTime(new Date());
logic.addCommentInfo("1", info);
logic.addCommentInfo("2", info);
}
但是,现在通过web请求,做如下测试.
请求URL:
http://localhost:8081/comment_service_webv3/service/CmmService/Add?参数串将logic代码稍做调整.public String addCommentInfo(String devID, CommentInfo commentInfo) {
Map<String, Object> params = new HashMap<String, Object>();
Dev dev = devDao.findByParams(params);
if(dev != null && !StringHelper.Empty.equals(dev.getServerID())){
//变库
DynamicDataSource dynamicDataSource = (DynamicDataSource) ApplicationContextHelper.getBean("dynDataSource");
//将数据库强制变为2库.
dynamicDataSource.selectDataSource(2);
}
commentInfo = commentInfoDao.insert(commentInfo);
return "";
}结果:
预期该条数据应该查入2库,但实际却仍然插入spring配置的默认datasource库(<property name="defaultTargetDataSource" ref="dataSource" />)里面.思考:这不科学呀,我两种方法基本没有区别呀.最后经过比对,找了大概出错的地方,是数据库层getSession出的不一样,但是不知道是为什么.第一种通过代码测试,debug截图如下图,发现其获得session中有动态添加进去的datasource.
但是通过web请求过来的,然后获得session里面,就没有动态添加进去的datasource,所以只能操作默认配置的数据库了,如下图
实在是想不通,两都方式对logic类来说,都是spring里面得到的对象,为什么结果会不一样呢.请大家指点,为什么?应当如何修改?
谢谢.
加上之后,代码测试也不行了.public static void main(String... args){
CommentInfoLogic logic = (CommentInfoLogic) ApplicationContextHelper.getBean("commentInfoLogic");
CommentInfo info = new CommentInfo();
info.setId(DateHelper.getUUID());
info.setCreateTime(new Date());
logic.addCommentInfo("1", info);
logic.addCommentInfo("2", info);
}
结果两条数据都是插入到了默认数据库了.这样就和web请求是一样的结果.
就是获得的session里面没有动态添加进去的datasource了.这是那里的原因呢.好像差一点点,就能解决了,就是不能捅破这最后一层纸呀.郁闷哟.
已经看到了,就是getSession出来的那个SqlSeesion里面的_targetDataSources没有保存,我后来动态set进去的datasource.
http://jijun87120681.iteye.com/blog/1320799
ApplicationContextHelper在XML中注册没