事务回滚的问题
public void edit(){ int userId = getParaToInt("id"); Integer[] groupIds = getParaValuesToInt("groupId"); Integer[] memuIds = getParaValuesToInt("memuId"); String name = getPara("name"); int login = getParaToInt("login"); int userType = getParaToInt("userType"); String groupIdStr = ToString.toString(groupIds); String memuIdStr = ToString.toString(memuIds); UserService.me.edit(userId, name, login, userType, groupIdStr, memuIdStr); redirect("/user/getUserList/"+userId); }
其中UserService.me.edit包括
@Before(Tx.class) public void edit(int userId, String name, int login,int userType,String groupIds,String memuIds) { User user = new User(); user.set("id", userId).set("name", name).set("login", login).set("userType", userType).update(); MemuService.me.updateMemuId(userId, memuIds, 2); GroupService.me.updateGroupId(userId, groupIds, 2); }
我让User的update成功,MemuService.me.updateMemuId失败
public void updateMemuId(int userId,String memuId,int type){ MemuRule rule = getMemuRule(userId); MemuRule r = new MemuRule(); if(rule==null){ r.set("groupId", memuId).set("userId", userId).save(); return; } String memuIds = rule.getMemuId(); if(type==1){ if(memuIds!=null&&memuIds.length()>2){ memuIds.replace(","+memuId, ""); } else { memuIds.replace(memuId, ""); } r.set("id", rule.getId()).set("memuId", memuIds).update(); } else { r.set("id", rule.getId()).set("memuId", memuId).update(); } }
public void updateGroupId(int userId,String groupId,int type){ GroupRule rule = getRule(userId); GroupRule r = new GroupRule(); if(rule==null){ r.set("groupId", groupId).set("userId", userId).save(); return; } String groupIds = rule.getGroupId(); if(type==1){ //删除分组 if(groupIds!=null&&groupIds.length()>2){ groupIds.replace(","+groupId, ""); } else { groupIds.replace(groupId, ""); } r.set("id", rule.getId()).set("groupId", groupIds).update(); } else { //修改分组 r.set("id", rule.getId()).set("groupId", groupId).update(); } }
最后user还是更新成功了。
/** * 配置全局拦截器 */ public void configInterceptor(Interceptors me) { me.add(new CheckInterceptor()); me.add(new SessionInViewInterceptor()); me.add(new TxByMethodRegex("(.*save.*|.*update.*)")); me.add(new TxByMethods("save", "update")); me.add(new TxByActionKeyRegex("/trans.*")); me.add(new TxByActionKeys("/tx/save", "/tx/update")); }
项目:JFinal
Db.tx(new IAtom() {
@Override
public boolean run() throws SQLException {
User user = new User();
boolean flag1 = user.set("id", userId).set("name", name).set("login", login).set("userType", userType).update();
boolean flag2 = MemuService.me.updateMemuId(userId, memuIds, 2);
boolean flag3 = GroupService.me.updateGroupId(userId, groupIds, 2);
return flag1&&flag2&&flag3;
}
});
}
修改成这种事务是可以回滚的,声明式事务怎么使用