NutzCN Logo
问答 NutzBoot使用事务的问题
发布于 1942天前 作者 文涛(wentao) 1501 次浏览 复制 上一个帖子 下一个帖子
标签:

使用事务提交数据后,马上用dao取数据,还是旧数据,代码如下

        NutTxDao tx = null;
        String updatePlatFormSql = "update account_extend set login_result = @login_result, plat_form=@plat_form, plat_form_lock = @plat_form_lock, area_code = @area_code, area_code_lock = @area_code_lock where account_id = @account_id";
        String insertPlatFormSql = "insert into account_extend (account_id, login_result, plat_form, plat_form_lock, area_code, area_code_lock) values (@account_id, @login_result, @plat_form, @plat_form_lock, @area_code, @area_code_lock)";
        try {
            tx = new NutTxDao(dao);
            tx.beginRC();

            // 判断是新增还是更新
            Sql checkPlatFormSql = Sqls.fetchInt("select count(1) from account_extend where account_id = @account_id");
            checkPlatFormSql.setParam("account_id", account_id);
            tx.execute(checkPlatFormSql);
            int checkPlatForm = checkPlatFormSql.getInt();
            Sql insertOrUpdatePlatFormSql = null;
            if(checkPlatForm > 0) {
                insertOrUpdatePlatFormSql = Sqls.create(updatePlatFormSql);
            } else {
                insertOrUpdatePlatFormSql = Sqls.create(insertPlatFormSql);
            }
            insertOrUpdatePlatFormSql.setParam("account_id", account_id);
            insertOrUpdatePlatFormSql.setParam("plat_form", platForm);
            insertOrUpdatePlatFormSql.setParam("plat_form_lock", platFormLock);
            if(login_result >= 0) {
                insertOrUpdatePlatFormSql.setParam("login_result", login_result);
            }
            if(!Strings.isBlank(areaCode)) {
                insertOrUpdatePlatFormSql.setParam("area_code", areaCode);
                insertOrUpdatePlatFormSql.setParam("area_code_lock", areaCodeLock);
            }
            tx.execute(insertOrUpdatePlatFormSql);
            tx.commit(); // 这里提交了数据库事务
            Sql sql = Sqls.fetchRecord("select * from account left join account_extend on account.id = account_extend.account_id where account.id = @account_id");
            sql.setParam("account_id", accountId);
            dao.execute(sql);
            Record map = sql.getObject(Record.class); // 这里查询出来的数据是更新前的
            log.debugf("Account_Record: %s", JSON.toJSONString(map));
        } catch (Exception e) {
            e.printStackTrace();
            tx.rollback();
        } finally {
            if(tx != null) {
                tx.close();
            }
        }
6 回复

在try catch之后取

commit之后是不会再回滚的,你这样写没意义的

Trans.DEBUG设置为true,可以观察事务日志

我就是commit之后确认不会回滚了我再去取的,你意思是可以在try里加个状态,如果commit了,在finally之后再去取是不

那就奇怪了, 数据库变了吗?

找到问题了,主从还没同步,延迟一会读取就好了。。。尴尬

我是写主库,读从库,读取的时候从库还没被同步更新

添加回复
请先登陆
回到顶部