• MySQL Crash Course #18# Chapter 26. Managing Transaction Processing


    InnoDB 支持 transaction ,MyISAM 不支持。

    索引:

    1. Changing the Default Commit Behavior
    2. SAVEPOINT 与 ROLLBACK TO
    3.  COMMIT 与 ROLLBACK

    When working with transactions and transaction processing, there are a few keywords that'll keep reappearing. Here are the terms you need to know:

    • Transaction A block of SQL statements

    • Rollback The process of undoing specified SQL statements

    • Commit Writing unsaved SQL statements to the database tables

    • Savepoint A temporary placeholder in a transaction set to which you can issue a rollback (as opposed to rolling back an entire transaction)

    有点类似于游戏里的存档,我们在什么时候需要存档呢?

    一般是在挑战 BOSS 之前、或者结束游戏的时候,总之是很重要的事情、而且失败之后代价很巨大。我们可以把 挑战 BOSS 这件事情叫做“事务transaction”。在挑战前的那个存档时间点叫做“保存点savepoint”,如果你 GG 了重新去读档就是“回滚rollback”了。

    再比如说,实际生活中给人转账,在我确认转账之后,银行系统首先会检查我的账户上有没有足够的钱,然后减去我要转出的金额,如果在这个时候银行系统突然宕机了(假设),那转出去的钱是不是不翼而飞了呢?。。。显然不会,银行系统在做这一系列事情之前肯定会预先存档,万一有意外直接读档回归到什么都没有做的时候就好了。

    下面是一个简单的例子:

    mysql> SELECT * FROM pet;
    +--------+----------+-----------+-----------+
    | pet_id | pet_type | pet_name  | master_id |
    +--------+----------+-----------+-----------+
    |   8881 | NULL     | 楗?《     |      1001 |
    |   8882 | dog      | 灏忕櫧    |      1002 |
    |   8883 | cat      | 鑰侀粍    |      1003 |
    +--------+----------+-----------+-----------+
    3 rows in set (0.00 sec)
    
    mysql> START TRANSACTION; -- 存档,下面要开始做重要的事情了
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> DELETE FROM pet;
    Query OK, 3 rows affected (0.00 sec)
    
    mysql> SELECT * FROM pet;
    Empty set (0.00 sec)
    
    mysql> ROLLBACK; -- 读档
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> SELECT * FROM pet;
    +--------+----------+-----------+-----------+
    | pet_id | pet_type | pet_name  | master_id |
    +--------+----------+-----------+-----------+
    |   8881 | NULL     | 楗?《     |      1001 |
    |   8882 | dog      | 灏忕櫧    |      1002 |
    |   8883 | cat      | 鑰侀粍    |      1003 |
    +--------+----------+-----------+-----------+
    3 rows in set (0.00 sec)

     记住ROLLBACK只能用于回滚 UPDATE、DELETE、INSERT ,而不能回滚 DROP 和 CREATE 。

    Changing the Default Commit Behavior

    As already explained, the default MySQL behavior is to automatically commit any and all changes. In other words, any time you execute a MySQL statement, that statement is actually being performed against the tables, and the changes made occur immediately. To instruct MySQL to not automatically commit changes, you need to use the following statement:

    SET autocommit=0;

    PS. 这个设置仅对当前连接有效,不是全局、持久的。

    默认状态下除非用了 

    START TRANSACTION;

    否则每一条 SQL;都是直接被提交的。

    看下面的例子:

    mysql> SET autocommit=0;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> SHOW VARIABLES LIKE "autocommit";
    +---------------+-------+
    | Variable_name | Value |
    +---------------+-------+
    | autocommit    | OFF   |
    +---------------+-------+
    1 row in set (0.00 sec)
    
    mysql> SELECT * FROM pet;
    +--------+----------+-----------+-----------+
    | pet_id | pet_type | pet_name  | master_id |
    +--------+----------+-----------+-----------+
    |   8881 | NULL     | 楗?《     |      1001 |
    |   8882 | dog      | 灏忕櫧    |      1002 |
    |   8883 | cat      | 鑰侀粍    |      1003 |
    +--------+----------+-----------+-----------+
    3 rows in set (0.00 sec)
    
    mysql> INSERT INTO pet(pet_name, master_id)
        -> VALUES ('老黄', 234);
    Query OK, 1 row affected (0.00 sec)
    
    mysql> SELECT * FROM pet; -- 说好不自动提交呢?。。。
    +--------+----------+-----------+-----------+
    | pet_id | pet_type | pet_name  | master_id |
    +--------+----------+-----------+-----------+
    |   8881 | NULL     | 楗?《     |      1001 |
    |   8882 | dog      | 灏忕櫧    |      1002 |
    |   8883 | cat      | 鑰侀粍    |      1003 |
    |   NULL | animal   | 老黄      |       234 |
    +--------+----------+-----------+-----------+
    4 rows in set (0.00 sec)

    就像上面所看到的,我们虽然没有 commit ,但改变还是发生了不是吗?。。但是实际上并没有“真正”提交,断开重连(或者同时再开启一个并行连接也行,可以用这个方法测试一下mysql的并发机制:当某个连接拿到某个表的锁之后,另外一个连接操作这张表就需要等待):

    mysql> ^CCtrl-C -- exit!
    Aborted
    root@xkfx:~# mysql -uroot -p
    mysql> USE mysqlCrash; SELECT * FROM pet;
    Database changed
    +--------+----------+-----------+-----------+
    | pet_id | pet_type | pet_name  | master_id |
    +--------+----------+-----------+-----------+
    |   8881 | NULL     | 楗?《     |      1001 |
    |   8882 | dog      | 灏忕櫧    |      1002 |
    |   8883 | cat      | 鑰侀粍    |      1003 |
    +--------+----------+-----------+-----------+
    3 rows in set (0.00 sec)

     SAVEPOINT 与 ROLLBACK TO

    SAVEPOINT delete1; -- 必须在事务内哦
    ROLLBACK TO delete1;  -- 在事务结束( ROLLBACK / ROLLBACK TO / COMMIT )之后,保存点会被自动释放,当然也可以通过  RELEASE SAVEPOINT 手动释放。

    COMMIT 与 ROLLBACK

    1、commit:整个事务的所有更改永久保存

      2、rollback:撤销事务所做的更改

    commit后不能回滚,但是如果你是9i或以上版本可以使用flashback来找回原来的数据。
    在一个事务中,rollback和commit都代表结束一个事务。要么回滚,要么提交。他们是在一个等级上的命令。rollback可以写在commit之前,但是commit的数据就不能rollback了。 --- radioxhk 

  • 相关阅读:
    storm源码分析之topology提交过程
    storm启动supervisor源码分析-supervisor.clj
    storm启动nimbus源码分析-nimbus.clj
    storm shell命令源码分析-shell_submission.clj
    storm定时器timer源码分析-timer.clj
    Storm在zookeeper上的目录结构
    storm操作zookeeper源码分析-cluster.clj
    Four subspaces
    拉格朗日量(函数)、达朗贝尔原理、哈密顿量
    离散正弦信号的周期
  • 原文地址:https://www.cnblogs.com/xkxf/p/8926098.html
Copyright © 2020-2023  润新知