一、什么是事务?
在人员管理系统中,你删除一个人员,你即需要删除人员的基本资料,也要删除和该人员相关的信息,如信箱,文章等等,这样,这些数据库操作语句就构成一个事务!
二、事务是必须满足4个条件(ACID)
- 事务的原子性( Atomicity):一组事务,要么成功;要么撤回。
- 一致性
(Consistency):事务执行后,数据库状态与其他业务规则保持一致。如转账业务,无论事务执行成功否,参与转账的两个账号余额之和应该是不变的。 - 隔离性(Isolation):事务独立运行。一个事务处理后的结果,影响了其他事务,那么其他事务会撤回。事务的100%隔离,需要牺牲速度。
- 持久性(Durability):软、硬件崩溃后,InnoDB数据表驱动会利用日志文件重构修改。可靠性和高速度不可兼得,
innodb_flush_log_at_trx_commit 选项 决定什么时候吧事务保存到日志里。
三、MySQL中的事务
在默认情况下,MySQL每执行一条SQL语句,都是一个单独的事务。如果需要在一个事务中包含多条SQL语句,那么需要开启事务和结束事务。
开启事务:start transaction
结束事务:commit或rollback
在执行SQL语句之前,先执行start transaction,这就开启了一个事务(事务的起点),然后可以去执行多条SQL语句,最后要结束事务,commit表示提交,即事务中的多条SQL语句所作出的影响会持久到数据库中,或者rollback,表示回滚到事务的起点,之前做的所有操作都被撤销了。
mysql> SELECT * FROM account;
+----+------+---------+
| id | NAME | balance |
+----+------+---------+
| 1 | zs | 1000.00 |
| 2 | ls | 1000.00 |
| 3 | ww | 1000.00 |
+----+------+---------+
3 rows in set (0.00 sec)
mysql> START TRANSACTION;
Query OK, 0 rows affected (0.00 sec)
mysql> UPDATE account SET balance=900 WHERE name = 'zs';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT * FROM account;
+----+------+---------+
| id | NAME | balance |
+----+------+---------+
| 1 | zs | 900.00 |
| 2 | ls | 1000.00 |
| 3 | ww | 1000.00 |
+----+------+---------+
3 rows in set (0.00 sec)
mysql> UPDATE account SET balance=1100 WHERE name = 'ls';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT * FROM account;
+----+------+---------+
| id | NAME | balance |
+----+------+---------+
| 1 | zs | 900.00 |
| 2 | ls | 1100.00 |
| 3 | ww | 1000.00 |
+----+------+---------+
3 rows in set (0.00 sec)
mysql> ROLLBACK;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT * FROM account;
+----+------+---------+
| id | NAME | balance |
+----+------+---------+
| 1 | zs | 1000.00 |
| 2 | ls | 1000.00 |
| 3 | ww | 1000.00 |
+----+------+---------+
3 rows in set (0.00 sec)
mysql> START TRANSACTION;
Query OK, 0 rows affected (0.00 sec)
mysql> UPDATE account SET balance=balance-100 WHERE name = 'zs';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT * FROM account;
+----+------+---------+
| id | NAME | balance |
+----+------+---------+
| 1 | zs | 900.00 |
| 2 | ls | 1000.00 |
| 3 | ww | 1000.00 |
+----+------+---------+
3 rows in set (0.00 sec)
mysql> UPDATE account SET balance=balance+100 WHERE name = 'ls';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT * FROM account;
+----+------+---------+
| id | NAME | balance |
+----+------+---------+
| 1 | zs | 900.00 |
| 2 | ls | 1100.00 |
| 3 | ww | 1000.00 |
+----+------+---------+
3 rows in set (0.00 sec)
mysql> commit;
Query OK, 0 rows affected (0.02 sec)
mysql> SELECT * FROM account;
+----+------+---------+
| id | NAME | balance |
+----+------+---------+
| 1 | zs | 900.00 |
| 2 | ls | 1100.00 |
| 3 | ww | 1000.00 |
+----+------+---------+
3 rows in set (0.00 sec)
四、JDBC事务
在JDBC中处理事务,都是通过Connection完成的。
同一事务中所有的操作,都在使用同一个Connection对象。
①JDBC中的事务
Connection的三个方法与事务有关:
setAutoCommit(boolean):设置是否为自动提交事务,如果true(默认值为true)表示自动提交,也就是每条执行的SQL语句都是一个单独的事务,如果设置为false,那么相当于开启了事务了;con.setAutoCommit(false) 表示开启事务。
commit():提交结束事务。
rollback():回滚结束事务。
try{
con.setAutoCommit(false);//开启事务
......
con.commit();//try的最后提交事务
} catch() {
con.rollback();//回滚事务
}
完整示例:
public static void main(String[] args) {
PreparedStatement pst=null;
Connection conn=null;
try {
conn = DBUtils.getConnection();
conn.setAutoCommit(false);//手动关闭事务
String sql1="update blank set money=money-1000 where id=1";
String sql2="update blank set money=money+1000 where id=2";
pst = conn.prepareStatement(sql1);
int n = pst.executeUpdate();//执行语句
pst = conn.prepareStatement(sql2);
n = pst.executeUpdate();//执行语句
conn.commit();//提交事务
if(n>0) {
System.out.println("修改成功");
}else {
System.out.println("修改失败");
}
} catch (Exception e) {
try {
conn.rollback(); //事务回滚
}
catch (SQLException e1) {
}
}finally {
DBUtils.myClose(conn, pst);
}
}