文章目录
相关免费学习推荐:mysql视频教程
一、MySQL的事务控制(Transaction Control Language)
(1)事务的特性(ACID)
事务指的是逻辑上的一组操作,组成这组操作的逻辑单元要么一起成功,要么一起失败。
(2)MySQL的事务控制
set autocommit = false;
rollback;
commit ;
(3)mysql事务演示
☆查看已经存在的emp表:
mysql> select * from emp;+-------+--------+--------+------+------------+----------+--------+--------+| empno | ename | job | mgr | hiredate | sal | commit | deptno |+-------+--------+--------+------+------------+----------+--------+--------+| 1002 | 白展堂 | clerk | 1001 | 1983-05-09 | 7000.00 | 200.00 | 10 || 1003 | 李大嘴 | clerk | 1002 | 1980-07-08 | 8000.00 | 100.00 | 10 || 1004 | 吕秀才 | clerk | 1002 | 1985-11-12 | 4000.00 | NULL | 10 || 1005 | 郭芙蓉 | clerk | 1002 | 1985-03-04 | 4000.00 | NULL | 10 || 1007 | 小白 | clerk | 1001 | 2019-11-25 | 5555.00 | 500.00 | NULL || 2001 | 胡一菲 | leader | NULL | 1994-03-04 | 15000.00 | NULL | 20 || 2002 | 陈美嘉 | manger | 2001 | 1993-05-24 | 10000.00 | 300.00 | 20 || 2003 | 吕子乔 | clerk | 2002 | 1995-05-19 | 7300.00 | 100.00 | 20 || 2004 | 张伟 | clerk | 2002 | 1994-10-12 | 8000.00 | 500.00 | 20 || 2005 | 曾小贤 | clerk | 2002 | 1993-05-10 | 9000.00 | 700.00 | 20 || 3001 | 刘梅 | leader | NULL | 1968-08-08 | 13000.00 | NULL | 30 || 3002 | 夏冬梅 | manger | 3001 | 1968-09-21 | 10000.00 | 600.00 | 30 || 3003 | 夏雪 | clerk | 3002 | 1989-09-21 | 8000.00 | 300.00 | 30 || 3004 | 张一山 | clerk | 3002 | 1991-06-16 | 8000.00 | 200.00 | 30 || 3007 | 嫦娥 | clerk | NULL | NULL | NULL | NULL | 10 |+-------+--------+--------+------+------------+----------+--------+--------+15 rows in set (0.00 sec)
①设置手动提交:
mysql> set autocommit=false;Query OK, 0 rows affected (0.03 sec)
②在emp表中插入一条ename为mary的语句:
mysql> insert into emp(ename,job,commit) -> values('mary','clerk',300);Query OK, 1 row affected (0.02 sec)mysql> select * from emp;+-------+--------+--------+------+------------+----------+--------+--------+| empno | ename | job | mgr | hiredate | sal | commit | deptno |+-------+--------+--------+------+------------+----------+--------+--------+| 1002 | 白展堂 | clerk | 1001 | 1983-05-09 | 7000.00 | 200.00 | 10 || 1003 | 李大嘴 | clerk | 1002 | 1980-07-08 | 8000.00 | 100.00 | 10 || 1004 | 吕秀才 | clerk | 1002 | 1985-11-12 | 4000.00 | NULL | 10 || 1005 | 郭芙蓉 | clerk | 1002 | 1985-03-04 | 4000.00 | NULL | 10 || 1007 | 小白 | clerk | 1001 | 2019-11-25 | 5555.00 | 500.00 | NULL || 2001 | 胡一菲 | leader | NULL | 1994-03-04 | 15000.00 | NULL | 20 || 2002 | 陈美嘉 | manger | 2001 | 1993-05-24 | 10000.00 | 300.00 | 20 || 2003 | 吕子乔 | clerk | 2002 | 1995-05-19 | 7300.00 | 100.00 | 20 || 2004 | 张伟 | clerk | 2002 | 1994-10-12 | 8000.00 | 500.00 | 20 || 2005 | 曾小贤 | clerk | 2002 | 1993-05-10 | 9000.00 | 700.00 | 20 || 3001 | 刘梅 | leader | NULL | 1968-08-08 | 13000.00 | NULL | 30 || 3002 | 夏冬梅 | manger | 3001 | 1968-09-21 | 10000.00 | 600.00 | 30 || 3003 | 夏雪 | clerk | 3002 | 1989-09-21 | 8000.00 | 300.00 | 30 || 3004 | 张一山 | clerk | 3002 | 1991-06-16 | 8000.00 | 200.00 | 30 || 3007 | 嫦娥 | clerk | NULL | NULL | NULL | NULL | 10 || 4008 | mary | clerk | NULL | NULL | NULL | 300.00 | NULL |+-------+--------+--------+------+------------+----------+--------+--------+16 rows in set (0.00 sec)
③使用rollback
回滚
mysql> rollback;Query OK, 0 rows affected (0.00 sec)mysql> select * from emp;+-------+--------+--------+------+------------+----------+--------+--------+| empno | ename | job | mgr | hiredate | sal | commit | deptno |+-------+--------+--------+------+------------+----------+--------+--------+| 1002 | 白展堂 | clerk | 1001 | 1983-05-09 | 7000.00 | 200.00 | 10 || 1003 | 李大嘴 | clerk | 1002 | 1980-07-08 | 8000.00 | 100.00 | 10 || 1004 | 吕秀才 | clerk | 1002 | 1985-11-12 | 4000.00 | NULL | 10 || 1005 | 郭芙蓉 | clerk | 1002 | 1985-03-04 | 4000.00 | NULL | 10 || 1007 | 小白 | clerk | 1001 | 2019-11-25 | 5555.00 | 500.00 | NULL || 2001 | 胡一菲 | leader | NULL | 1994-03-04 | 15000.00 | NULL | 20 || 2002 | 陈美嘉 | manger | 2001 | 1993-05-24 | 10000.00 | 300.00 | 20 || 2003 | 吕子乔 | clerk | 2002 | 1995-05-19 | 7300.00 | 100.00 | 20 || 2004 | 张伟 | clerk | 2002 | 1994-10-12 | 8000.00 | 500.00 | 20 || 2005 | 曾小贤 | clerk | 2002 | 1993-05-10 | 9000.00 | 700.00 | 20 || 3001 | 刘梅 | leader | NULL | 1968-08-08 | 13000.00 | NULL | 30 || 3002 | 夏冬梅 | manger | 3001 | 1968-09-21 | 10000.00 | 600.00 | 30 || 3003 | 夏雪 | clerk | 3002 | 1989-09-21 | 8000.00 | 300.00 | 30 || 3004 | 张一山 | clerk | 3002 | 1991-06-16 | 8000.00 | 200.00 | 30 || 3007 | 嫦娥 | clerk | NULL | NULL | NULL | NULL | 10 |+-------+--------+--------+------+------------+----------+--------+--------+15 rows in set (0.00 sec)
发现mary没有插入成功,因为手动提交后进行和回滚;如果使用commit进行提交就无法回滚成功,因为直接插入进数据库中了。
二、JDBC的事务控制
(1)JDBC事务简介
(2)JDBC事务的API
conn.setAutoCommit(false);//将JDBC事务设置手动提交conn.commit();conn.rollback();
(3)JDBC事务控制模拟
在emp表中修改ename名为hellen的记录,将其job从leader修改为clerk,奖金commit从600降低至300。
(模拟交易)TrasactionDemo:
package jdbc;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLException;import util.JdbcUtil;/** * JDBC事务控制 * * @author Administrator * */public class TestTrasaction { public static void main(String[] args) { Connection conn = null; PreparedStatement pstm = null; PreparedStatement pstm1 = null; try { // 获得连接 conn = JdbcUtil.getConnection(); //将JDBC事务设置手动提交 conn.setAutoCommit(false); // ①降职操作 String sql = "updata emp set job = 'clerk' where ename = 'hellen'"; pstm = conn.prepareStatement(sql); pstm.executeUpdate(); // ②降奖金操作 String sql1 = "updata emp set commit='3000' where ename = 'hellen'"; pstm1 = conn.prepareStatement(sql1); pstm1.executeUpdate(); //提交事务 conn.commit(); } catch (Exception e) { //事务回滚 try { conn.rollback(); } catch (SQLException e1) { e1.printStackTrace(); } } finally { try { JdbcUtil.release(null, pstm, null); JdbcUtil.release(null, pstm1, conn); } catch (Exception e) { e.printStackTrace(); } } }}
代码中添加了JDBC事务来处理两条sql语句(一条正确,一条错误),两条sql语句都没有执行。如果不添加JDBC事务处理,那么其中一条正确的sql语句就会单独执行。
相关免费学习推荐:mysql数据库(视频)
Atas ialah kandungan terperinci 介绍MySQL和JDBC的事务控制(TCL). Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!