1. 程式人生 > >取消默認提交,可以回滾

取消默認提交,可以回滾

blog ace etc comm 保存 oca new 自動提交 put

Connection conn = null;
try {
    conn = DbKit.getConfig().getConnection();//獲取連接
    DbKit.getConfig().setThreadLocalConnection(conn);
    conn.setAutoCommit(false);//自動提交變成false
//存儲操作
conn.commit();//提交保存,確定存入
Map map=new HashMap();
map.put("code",0);
map.put("msg",Consts.SAVE_SUCCESS);
renderJson(map);
} 
catch (Exception e) { try { if (null != conn) conn.rollback();//回滾 } catch (Exception e1) { e1.printStackTrace(); } Map map=new HashMap(); map.put("code",1); map.put("msg",Consts.SAVE_FAILED); renderJson(map); } finally { try { if (null != conn) {conn.close();} }
catch (Exception e2) { e2.printStackTrace(); } finally { DbKit.getConfig().removeThreadLocalConnection(); } }

取消默認提交,可以回滾