Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

两个连用的$.post中会解析不到数据,时好时坏,请问各位怎么解决? #873

Closed
516388532 opened this issue Jun 2, 2015 · 4 comments
Assignees
Milestone

Comments

@516388532
Copy link

  1. 前台写了两个连续的post请求,第一个获取数据总量,第二个获取分页的内容
    $.post('jsp/ware/wareinfo_countlike', data, function() { ... })
    $.post('jsp/ware/wareinfo_bypagelike', data, function() { ... })
    这样请求时,后台会出现有时读数据有时不读的情况。

但如果改成嵌套的post请求,单用户时可以避免这种情况,多用户时则仍然有隐患。
$.post('jsp/ware/wareinfo_countlike', data, function() {
$.post('jsp/ware/wareinfo_bypagelike', data, function() { ... })
})

  1. 上两个post请求对应的后台处理贴在了下面,关键的一处是:在使用NutzDao的fetchLinks获取关联对象时,有时会直接跳过而不执行。
    DEBUG时发现Nutz在visitOne中的getList时是从cache中获取关联对象的,是不是由于两个post请求并发导致的问题?其它

@IocBean
@at("/jsp/ware")
public class WareInfoAction {
// 模糊查询条数
@at("/wareinfo_countlike")
public Object wareinfoCountLike(String value){
return BasicDao.getInstance().searchPageByLike(WareInfo.class, value,"isdel",0);
}

// 模糊查询分页
@At("/wareinfo_bypagelike")
public List<WareInfo> wareinfoByPageLike(String value,int currentPage,int pageSize){
    List<WareInfo> list = BasicDao.getInstance().searchPageByLike(WareInfo.class,value,"isdel",0,currentPage,pageSize);

    // 获取一级关联表
    for(WareInfo qc : list){
        // 以下两个查询有时候会被选择性地忽略
        BasicDao.getInstance().getDao().fetchLinks(qc, "trayinfo"); 
        BasicDao.getInstance().getDao().fetchLinks(qc, "orderbom");
    }
    return list;
}

}

public class BasicDao {

...

private static NutzBasicDao uniqueInstance = new NutzBasicDao();
private Dao dao;

private NutzBasicDao() {
    dao = new NutIoc(new JsonLoader("/config/dao.js")).get(Dao.class);
}

public static NutzBasicDao getInstance() {
    return uniqueInstance;
}

public Dao getDao() {
    return dao;
}

public <T> int searchPageByLike(Class<T> c, String value, String fieldName,
        Integer value1) {
    Entity<T> entity = dao.getEntity(c);

    List<MappingField> fields = entity.getMappingFields();

    SqlExpressionGroup group = null;

    for (MappingField f : fields) {
        if (!f.isId()) {
            SqlExpression e = Cnd.exps(f.getColumnName(), "LIKE",
                    "%" + value + "%").and(fieldName, "=", value1);
            if (group == null) {
                group = Cnd.exps(e);
            } else {
                group.or(e);
            }
        }
    }

    return dao.count(c, Cnd.where(group));
}


public <T> List<T> searchPageByLike(Class<T> c, String value,
        String fieldName, Integer value1, int currentPage, int pageSize) {

    Entity<T> entity = dao.getEntity(c);
    List<MappingField> fields = entity.getMappingFields();
    SqlExpressionGroup group = null;

    for (MappingField f : fields) {
        if (!f.isId()) {
            SqlExpression e = Cnd.exps(f.getColumnName(), "LIKE",
                    "%" + value + "%").and(fieldName, "=", value1);
            if (group == null) {
                group = Cnd.exps(e);
            } else {
                group.or(e);
            }
        }
    }

    Pager pager = dao.createPager(currentPage, pageSize);

    return dao.query(c, Cnd.where(group), pager);
}

...

}

@wendal
Copy link
Member

wendal commented Jun 2, 2015

findLink 并非nutz内部的方法, 请自查

@516388532 516388532 changed the title 两个连用的$.post中若有两次以上findlink,会解析不到数据,时好时坏,请问各位怎么解决? 两个连用的$.post中会解析不到数据,时好时坏,请问各位怎么解决? Jun 2, 2015
@wendal
Copy link
Member

wendal commented Jun 2, 2015

把这段改一下试试

        for(WareInfo qc : list){
            wiDao.findLink(qc, "trayinfo");// 获取一级关联表
            wiDao.findLink(qc.getTrayinfo(), "stockLocationInfo");
            wiDao.findLink(qc, "orderbom");
            wiDao.findLink(qc.getOrderbom(),"order");  // 通过中间表获取二级关联表
            wiDao.findLink(qc.getOrderbom(),"drawInfo");
            rlist.add(qc);
        }

改成

        for(WareInfo qc : list){
            wiDao.findLink(qc, null);// 获取一级关联表
            wiDao.findLink(qc.getTrayinfo(), null);
            wiDao.findLink(qc.getOrderbom(),null); 
            rlist.add(qc);
        }

@wendal
Copy link
Member

wendal commented Jun 7, 2015

最终是通过在Setup.init中对所有pojo类进行getEntity操作解决

原因可能是NutEntity的生成过程并没有做到线程同步,待查

@wendal
Copy link
Member

wendal commented Jun 9, 2015

确定有这问题,是带关联属性的Entity, 在未完成全部操作的情况下,将自身set到EntityHolder,所以另外一个线程就会拿到一个不完整的Entity

@wendal wendal closed this as completed in 8236bf0 Jun 9, 2015
@wendal wendal added this to the 1.b.53 milestone Jun 9, 2015
@wendal wendal self-assigned this Jun 9, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants