day55
事务
MySQL:每一条语句都属于独立事务,默认自动管理提交的。
如果需要把多条语句当成一个整体,那么就需要把多条语句放在一个事务里面
开启事务:start transaction
提交事务:commit;
回滚事务:rollback
封装事务
JDBC操作事务1
1.模拟银行用户转账,就是对数控库账户的操作【最开始是两个事务,事务是默认自动管理提交】
会出现问题:当在两个事务之间加一个错误,就会出现脏数据【一个账务减了钱,另一个账户没有加钱】,还有项目崩掉也可能出现
- 将加减钱放在一个事务里面,也要考虑同一个连接对象的问题【连接对象的共享】;
public class Test01 {
/**
* 知识点:数据库知识点的梳理
* 1.数据库概念(数据库服务器、数据库、数据表、字段、数据行)
* 2.使用SQL(DDL、DML、DCL)
* 3.深入SQL(数据类型、约束、索引、视图、触发器、存储过程、函数)
* 4.JDBC(使用Java语法操作数据库的技术,Connection、Statement、ResultSet)
* 5.SQL注入
* 6.封装DBUtil - v1.0(使用配置文件 - DBConfig.properties)
* 7.封装DBUtil - v2.0(更新方法、主键回填方法、查询方法)
* 8.事务
* 9.封装DBUtil - v3.0(封装事务)
* 10.批处理
* 11.CBLob - 了解
* 12.自定义连接池
* 13.Druid连接池
* 14.封装DBUtil - v3.0(封装连接池)
*
* 知识点:使用JDBC操作事务
* 需求:模拟银行用户转账
*/
public static void main(String[] args) {
Connection connection = nu
PreparedStatement statement1 = null;
PreparedStatement statement2 = null;
try {
connection = DBUtil.getConnection();
//开启事务
connection.setAutoCommit(false);
String sql1 = "UPDATE bank SET money=money-200 WHERE id=1;";
statement1 = connection.prepareStatement(sql1);
statement1.executeUpdate();
System.out.println(10/0);
String sql2 = "UPDATE bank SET money=money+200 WHERE id=2;";
statement2 = connection.prepareStatement(sql2);
statement2.executeUpdate();
//提交事务
connection.commit();
} catch (Exception e) {
//回滚事务
if(connection != null){
try {
connection.rollback();
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
}
} finally {
DBUtil.close(null,statement2,null);
DBUtil.close(connection,statement1,null);
}
}
}
JDBC操作事务2
对操作1中代码升级
这里设计存钱和取钱的方法时,改用抛出异常,让调用方去处理
public class Test01 {
/**
* 知识点:使用JDBC操作事务
* 需求:模拟银行用户转账
* 程序设计的要求:
* 设计存钱和取钱的方法
* public static void saveMoney(int id,float money) -- 存钱
* public static void withdrawMoney(int id,float money) -- 取钱
* 单独调用存钱和取钱的方法
* 联合调用存钱和取钱的方法 -- 转账
*/
public static void main(String[] args) {
// //场景一:单独调用存钱和取钱的方法
// try {
// saveMoney(1,200);
// } catch (SQLException e) {
// throw new RuntimeException(e);
// }
//
// try {
// withdrawMoney(2,300);
// } catch (SQLException e) {
// throw new RuntimeException(e);
// }
//场景二:转账
try {
DBUtil.startTransaction();
withdrawMoney(1,200);
//System.out.println(10/0);
saveMoney(2,200);
DBUtil.commit();
} catch (Exception e) {
try {
DBUtil.rollback();
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
}
}
public static void withdrawMoney(int id,float money) throws SQLException {
Connection connection = null;
PreparedStatement statement = null;
try {
connection = DBUtil.getConnection();
System.out.println(connection);
String sql = "update bank set money=money-? where id=?";
statement = connection.prepareStatement(sql);
statement.setFloat(1,money);
statement.setInt(2,id);
statement.executeUpdate();
} finally {
DBUtil.close(connection,statement,null);
}
}
public static void saveMoney(int id,float money) throws SQLException {
Connection connection = null;
PreparedStatement statement = null;
try {
connection = DBUtil.getConnection();
System.out.println(connection);
String sql = "update bank set money=money+? where id=?";
statement = connection.prepareStatement(sql);
statement.setFloat(1,money);
statement.setInt(2,id);
statement.executeUpdate();
} finally {
DBUtil.close(connection,statement,null);
}
}
}
连接对象比较: