129.事务介绍
基本介绍:
-
JDBC程序中当一个Connection对象创建时,默认情况下是自动提交事务:每次执行一个SQL语句时,如果执行成功,就会向数据库自动提交,而不能回滚
-
JDBC程序中为了让多个SQL语句作为一个整体执行,需要使用事务
-
调用Connection的setAutoCommit(false)可以取消自动提交事务
-
在所有的SQL语句都成功执行后,调用 Conncetion 的 commit();方法提交事务
-
在其某个操作失败或出现异常时,调用 Conncetion 的 rollback();方法回滚事务
130.事务处理
不使用事务可能出现的问题模拟:
public class TestJava {
//模拟经典的转账业务
public void noTransaction() {
//操作转账业务
//1.得到链接
Connection connection = null;
//2.组织一个sql
String sql = "update account set balance = balance - 100 where id = 1";
String sql2 = "update account set balance = balance + 100 where id = 2";
PreparedStatement preparedStatement = null;
//3.创建PreparedStatement 对象
try {
connection = JDBCUtils.getConnection();//在默认情况下自动提交
preparedStatement = connection.prepareStatement(sql);
preparedStatement.executeUpdate();//执行第一条sql
int i = 1/0;//抛出异常
preparedStatement = connection.prepareStatement(sql2);
preparedStatement.executeUpdate();//执行第二条sql
} catch (SQLException e) {
e.printStackTrace();
} finally {
//关闭资源
JDBCUtils.close(null,preparedStatement,connection);
}
}
}
//因为有异常,所以账户1 -100提交成功,但账户2 +100却没有提交成功
用事务解决:
public class TestJava {
//模拟经典的转账业务
public void Transaction() {
//操作转账业务
//1.得到链接
Connection connection = null;
//2.组织一个sql
String sql = "update account set balance = balance - 100 where id = 1";
String sql2 = "update account set balance = balance + 100 where id = 2";
PreparedStatement preparedStatement = null;
//3.创建PreparedStatement 对象
try {
connection = JDBCUtils.getConnection();//在默认情况下自动提交
//将 connection 设置为不自动提交
connection.setAutoCommit(false);//开始了事务
preparedStatement = connection.prepareStatement(sql);
preparedStatement.executeUpdate();//执行第一条sql
int i = 1/0;//抛出异常
preparedStatement = connection.prepareStatement(sql2);
preparedStatement.executeUpdate();//执行第二条sql
//这里提交事务
connection.commit();
} catch (SQLException e) {
//这里我们可以进行回滚,即撤销执行的sql
//默认回滚到事务开始的状态
System.out.println("执行发生了异常,回滚并撤销事务");
try {
connection.rollback();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
e.printStackTrace();
} finally {
//关闭资源
JDBCUtils.close(null,preparedStatement,connection);
}
}
}
131.批处理应用
基本介绍:
1.当需要成批插入或者更新记录时,可以采用Java的批量跟新机制,这一机制允许多条语句一次性提交给数据库批量处理
2.JDBC的批量处理语句包括下面方法
addBatch():添加需要批量处理的SQL语句或参数
executeBatch():执行批量处理语句
clearBatch():清空批处理包的语句
3.JDBC连接MYSQL时,如果要使用批量处理功能,请在url中加参数 ?rewriteBatchedStatements=ture
4.批处理往往和PreparedStatement一起搭配使用,可以既减少编译次数,又减少运行次数,效率大大提高
代码
public class TestJava {
//演示Java的批处理
//传统方法,添加5000条数据到admin2
public void noBatch() throws SQLException {
Connection connection = JDBCUtils.getConnection();
String sql = "insert into admin2 values (null, ?, ?)";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
System.out.println("开始执行");
long start = System.currentTimeMillis();//开始时间
for (int i = 0; i < 5000; i++) {
preparedStatement.setString(1,"jack" + i);
preparedStatement.setString(2,"666");
preparedStatement.executeUpdate();
}
long end = System.currentTimeMillis();
System.out.println("传统方式 耗时=" + (end - start));
//关闭连接
JDBCUtils.close(null,preparedStatement,connection);
}
//使用批量方式添加数据
public void batch() throws SQLException {
Connection connection = JDBCUtils.getConnection();
String sql = "insert into admin2 values (null, ?, ?)";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
System.out.println("开始执行");
long start = System.currentTimeMillis();//开始时间
for (int i = 0; i < 5000; i++) {
preparedStatement.setString(1,"jack" + i);
preparedStatement.setString(2,"666");
//将sql 语句加入到批处理包
preparedStatement.addBatch();
//当有1000条记录时,再批量执行
if ((i + 1) % 1000 == 0) {//满1000条就批量执行
preparedStatement.executeBatch();
//清空一把
preparedStatement.clearBatch();
}
}
long end = System.currentTimeMillis();
System.out.println("批量方式 耗时=" + (end - start));//108
//关闭连接
JDBCUtils.close(null,preparedStatement,connection);
}
}
===============mysql.properties==============
user=root
password=12345
url=jdbc:mysql://localhost:3306/db02?rewriteBatchedStatement=true
driver=com.mysql.jdbc.Driver



















![[WMCTF2020]Make PHP Great Again 2.01](https://i-blog.csdnimg.cn/direct/9bef470f85ef4304906764a6c22b256f.png)