大家好,又见面了,我是你们的朋友全栈君。
数据一旦提交,就不可回滚 那些操作会导致数据的自动提交?
Connection connection = DriverManager.getConnection(url, user, password);
connection.setAutoCommit(false);
String sql1 = "update user_table set balance = balance - 100";
update(connection,sql1,"AA");
String sql2 = "update user_table set balance = balance + 100"
update(connection,sql2,"BB");
connection.commit();
注意 若Connection没有被关闭,还有可能被重复使用,则需要恢复其自动提交状态。尤其是在使用数据库连接池技术的时候。
事务的ACID属性
四种隔离级别
java中的事务操作举例
Connection connection = DriverManager.getConnection(url, user, password);
//获取当前数据库的隔离级别
System.out.println(connection.getTransactionIsolation());
//设置数据库的隔离级别
connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
connection.setAutoCommit(false);
String sql = "select user,password,balance = ? from user_table where user = ?";
User user = getInstance(connection,User.class,sql,"CC");
System.out.println();
Connection connection = DriverManager.getConnection(url, user, password);
connection.setAutoCommit(false);
String sql = "update user_table set balance = ? where user = ?";
update(connection,sql,5000,"CC");
Thread.sleep(15000);
System.out.println("修改结束");
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/168821.html原文链接:https://javaforall.cn