Article Directory
Related free learning recommendations: mysql video tutorial
(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.Set manual submission:
Rollback
Commit
☆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)
①Set manual submission:
mysql> set autocommit=false;Query OK, 0 rows affected (0.03 sec)
②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)
③Use
rollbackRollback<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> 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.
(1)JDBC transaction introduction
Default transaction Commit strategy: A command constitutes a complete transaction by itself.
conn.setAutoCommit(false);//将JDBC事务设置手动提交conn.commit();conn.rollback();
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:
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 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.
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!