Java高级Day49-事务和批量处理

embedded/2024/9/23 5:51:33/

129.事务介绍

基本介绍:

  1. JDBC程序中当一个Connection对象创建时,默认情况下是自动提交事务:每次执行一个SQL语句时,如果执行成功,就会向数据库自动提交,而不能回滚

  2. JDBC程序中为了让多个SQL语句作为一个整体执行,需要使用事务

  3. 调用Connection的setAutoCommit(false)可以取消自动提交事务

  4. 在所有的SQL语句都成功执行后,调用 Conncetion 的 commit();方法提交事务

  5. 在其某个操作失败或出现异常时,调用 Conncetion 的 rollback();方法回滚事务

130.事务处理

不使用事务可能出现的问题模拟:

public class TestJava {//模拟经典的转账业务public void noTransaction() {//操作转账业务//1.得到链接Connection connection = null;
​//2.组织一个sqlString 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.组织一个sqlString 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条数据到admin2public 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

http://www.ppmy.cn/embedded/115438.html

相关文章

C++ STL之队列queue和双端队列deque

一. 概述 1.1 queue std::queue 是 C STL 中的一个容器适配器&#xff0c;用于实现先进先出&#xff08;FIFO&#xff0c;First In First Out&#xff09;的数据结构&#xff0c;它允许在一端添加元素&#xff08;称为队尾&#xff09;&#xff0c;并在另一端移除元素&#x…

Anaconda 安装与使用教程

1. 介绍 Anaconda 是一个用于科学计算的 Python 和 R 的发行版&#xff0c;它包含了众多流行的科学、数学、工程和数据分析包。Anaconda 是完全免费的&#xff0c;并且适用于 Windows、Mac 和 Linux 平台。它不仅是一个发行版&#xff0c;还提供了一个环境管理系统&#xff0c…

WPF 异步

在 WPF 中&#xff0c;异步编程非常重要&#xff0c;尤其是为了保持 UI 线程的响应性。由于 WPF 的 UI 操作必须在主线程上进行&#xff0c;耗时的任务&#xff08;如文件读写、网络请求等&#xff09;如果直接在 UI 线程上执行&#xff0c;会导致 UI 冻结&#xff0c;界面无法…

前端组件库Element UI 的使用

一、准备工作 1.确保安装了开发软件 VS Code&#xff08;此处可查阅安装 VS Code教程&#xff09;&#xff0c;确保相关插件安装成功 2.安装Node.js 和创建Vue项目&#xff08;此处可查阅安装创建教程&#xff09; 3.成功在VS Code运行一个Vue项目&#xff08;此处可查阅运行…

【练习16】求最小公倍数

链接&#xff1a;求最小公倍数_牛客题霸_牛客网 (nowcoder.com) 题目分析&#xff1a; 要求最小公倍数&#xff0c;要先用辗转相除法求最大公约数。假如有两个数a、b&#xff1a; 最小公倍数a*b / a和b的最大公约数 最大公约数 &#xff08;b, a % b&#xff09;&#xff0c;直…

第十一章 【后端】商品分类管理微服务(11.3)——商品管理模块 yumi-etms-goods

11.3 商品管理模块 yumi-etms-goods 新建 yumi-etms-goods 模块 添加依赖 pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns&#

外包干了4年,技术退步太明显了。。。。。

先说一下自己的情况&#xff0c;本科生生&#xff0c;20年通过校招进入武汉某软件公司&#xff0c;干了差不多4年的功能测试&#xff0c;今年国庆&#xff0c;感觉自己不能够在这样下去了&#xff0c;长时间呆在一个舒适的环境会让一个人堕落!而我已经在一个企业干了四年的功能…

【Taro】初识 Taro

笔记来源&#xff1a;编程导航。 概述 Taro 官方文档&#xff1a;https://taro-docs.jd.com/docs/ &#xff08;跨端开发框架&#xff09; Taro 官方框架兼容的组件库&#xff1a; taro-ui&#xff1a;https://taro-ui.jd.com/#/ &#xff08;最推荐&#xff0c;兼容性最好&…