JDBC中的事务
发布日期:2021-04-30 21:06:32 浏览次数:92 分类:精选文章

本文共 1621 字,大约阅读时间需要 5 分钟。

ACID???????????????????????????????????????????????????????????????????????????????????????????

??????

???????????????????????????????????

  • ???Dirty Read????????????????????????
  • ??????Non-Repeatable Read??????????????????????????????
  • ??????Ghost Read??????????????????????????????

Java??????

???????Java??????????

public class TestTransaction {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = JdbcUtils.getConnection();
conn.setAutoCommit(false); // ????
String sql1 = "update account set money = money - 100 where name = 'A'";
ps = conn.prepareStatement(sql1);
ps.executeUpdate();
// ????
int x = 1 / 0; // ????
String sql2 = "update account set money = money + 100 where name = 'B'";
ps = conn.prepareStatement(sql2);
ps.executeUpdate();
conn.commit(); // ??????
System.out.println("???");
} catch (SQLException e) {
try {
conn.rollback(); // ???????
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
} finally {
JdbcUtils.release(conn, ps, rs);
}
}
}

????

  • ????????????conn.setAutoCommit(false);???????????????????????????
  • ?????????????????????????
  • ?????
    • ??????try-catch??????SQLException?
    • ?catch???????????????????????????
  • ??????finally???????????????????????
  • ??????????????????????????????????????rollback()????????

    上一篇:Leetcode--837. 新21点(java)
    下一篇:掌握这十个搜索技巧让你的工作效率至上提高十倍!内容太过真实

    发表评论

    最新留言

    留言是一种美德,欢迎回访!
    [***.207.175.100]2026年06月16日 12时02分00秒