当前位置:编程学习 > JAVA >>

Spring+myBatis的动态多数据源 未能成功切换

--------------------编程问答-------------------- /////////////////////////////////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"看下。 --------------------编程问答--------------------
引用 2 楼 fangmingshijie 的回复:
给id="dynDataSource"的bean加上scope="prototype"看下。


好的,马上试试.

谢谢 --------------------编程问答--------------------
引用 2 楼 fangmingshijie 的回复:
给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了.

这是那里的原因呢.

好像差一点点,就能解决了,就是不能捅破这最后一层纸呀.郁闷哟.
--------------------编程问答-------------------- debug下,看哪一步有问题。 --------------------编程问答--------------------
引用 5 楼 fangmingshijie 的回复:
debug下,看哪一步有问题。


已经看到了,就是getSession出来的那个SqlSeesion里面的_targetDataSources没有保存,我后来动态set进去的datasource. --------------------编程问答-------------------- 这是为什么呢?不科学呀,

求科普 --------------------编程问答-------------------- 没有人来科谱呀. --------------------编程问答-------------------- 楼主最后怎么解决的 --------------------编程问答-------------------- 没有找到方法解决 --------------------编程问答-------------------- 看看这个
http://jijun87120681.iteye.com/blog/1320799
ApplicationContextHelper在XML中注册没 --------------------编程问答-------------------- 楼主,这个问题你解决了吗?
有没有代码 .提供一份呢?谢谢!!! --------------------编程问答-------------------- 1、消息中间件,能解决

2、 private Map<Object, Object> ,以前写的时候,第二个参数定义的是sessionfactory ,但当时的系统是统一的数据库 --------------------编程问答--------------------
引用 12 楼 wef 的回复:
楼主,这个问题你解决了吗?
有没有代码 .提供一份呢?谢谢!!!


代码基本上都贴出来了 --------------------编程问答--------------------
引用 13 楼 yeness 的回复:
1、消息中间件,能解决

2、 private Map<Object, Object> ,以前写的时候,第二个参数定义的是sessionfactory ,但当时的系统是统一的数据库


我的也全部是mysql --------------------编程问答-------------------- --------------------编程问答-------------------- 楼主问题解决了没有 能分享一下么 ?
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,