本帖最后由 ilyzj 于 2013-01-30 20:45:20 编辑

解决方案 »

  1.   

    /////////////////////////////////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里面得到的对象,为什么结果会不一样呢.请大家指点,为什么?应当如何修改?
    谢谢.
      

  2.   

    给id="dynDataSource"的bean加上scope="prototype"看下。
      

  3.   


    加上之后,代码测试也不行了.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了.这是那里的原因呢.好像差一点点,就能解决了,就是不能捅破这最后一层纸呀.郁闷哟.
      

  4.   


    已经看到了,就是getSession出来的那个SqlSeesion里面的_targetDataSources没有保存,我后来动态set进去的datasource.
      

  5.   

    看看这个
    http://jijun87120681.iteye.com/blog/1320799
    ApplicationContextHelper在XML中注册没
      

  6.   

    1、消息中间件,能解决2、 private Map<Object, Object> ,以前写的时候,第二个参数定义的是sessionfactory ,但当时的系统是统一的数据库