使用事务提交数据后,马上用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();
}
}