取消默认提交,可以回滚

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();
    }
}

 

posted @ 2017-08-30 11:07  易之盛  阅读(115)  评论(0编辑  收藏  举报