取消默认提交,可以回滚

Posted 易之盛技术团队-成长之路

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了取消默认提交,可以回滚相关的知识,希望对你有一定的参考价值。

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

 

以上是关于取消默认提交,可以回滚的主要内容,如果未能解决你的问题,请参考以下文章