Home > Database > Mysql Tutorial > body text

Introducing transaction control (TCL) for MySQL and JDBC

coldplay.xixi
Release: 2021-03-30 10:20:40
forward
1983 people have browsed it

Introducing transaction control (TCL) for MySQL and JDBC

Article Directory

  • 1. MySQL Transaction Control Language
    • (1)Transaction characteristics (ACID)
    • (2)MySQL transaction control
    • (3)mysql transaction demonstration
  • ## 2. JDBC transaction control
    • (1)JDBC transaction introduction
    • (2)JDBC transaction API
    • (3)JDBC transaction control simulation

Related free learning recommendations: mysql video tutorial


# #1. MySQL’s transaction control (Transaction Control Language)

(1) Transaction characteristics (ACID)

Transaction refers to a logical A group of operations in which the logical units that make up the group of operations either succeed together or fail together.

Atomicity: Emphasizes the indivisibility of transactions.
  • Consistency (consistency): It emphasizes that the integrity of the data must be maintained before and after the execution of the transaction.
  • Isolation: The execution of a transaction should not be interfered with by other transactions.
  • Durability (durability): Once the transaction ends (commit/rollback), the data is persisted to the database.
(2)MySQL transaction control

Set manual submission:
    set autocommit = false;
  • Rollback
  • rollback;
  • Commit
  • commit ;
(3)mysql transaction demonstration

☆View the existing emp table:

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)
Copy after login

①Set manual submission:

mysql> set autocommit=false;Query OK, 0 rows affected (0.03 sec)
Copy after login

②Insert a statement with ename as mary in the emp table:

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)
Copy after login

③Use

rollback

Rollback<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">mysql> rollback;Query OK, 0 rows affected (0.00 sec)mysql&gt; 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)</pre><div class="contentsignin">Copy after login</div></div>It is found that mary was not inserted successfully because it was manually submitted and rolled back; if commit is used to submit, the rollback cannot be successful because it is directly inserted into the database. bingo.


2. JDBC transaction control

(1)JDBC transaction introduction

Default transaction Commit strategy: A command constitutes a complete transaction by itself.
  • Requirements: Each logical unit either succeeds together or fails together. (Such as transfer)
(2)JDBC transaction API

conn.setAutoCommit(false);//将JDBC事务设置手动提交conn.commit();conn.rollback();
Copy after login

(3)JDBC transaction control simulation

Modify the record whose ename is hellen in the emp table, change its job from leader to clerk, and reduce the bonus commit from 600 to 300.


(Simulated transaction) TrasactionDemo: Introducing transaction control (TCL) for MySQL and JDBC

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();
			}
		}
	}}
Copy after login

JDBC transactions are added to the code to process two sql statements (one correct, one wrong), two None of the sql statements were executed. If you do not add JDBC transaction processing, then one of the correct sql statements will be executed alone. Introducing transaction control (TCL) for MySQL and JDBC

Related free learning recommendations:

mysql database(Video)

The above is the detailed content of Introducing transaction control (TCL) for MySQL and JDBC. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!