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.
Set manual submission:
- set autocommit = false;
-
Rollbackrollback; -
Commitcommit ;
☆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<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>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.
- Requirements: Each logical unit either succeeds together or fails together. (Such as transfer)
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!
Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AMInnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.
MySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AMCompared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.
Learning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AMMySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.
MySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AMMySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.
MySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AMMySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.
MySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AMMySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.
The Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AMSQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.
MySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AMThe basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool






