暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

OceanBase Cloud快速入门第86期:如何提交事务?

欢迎访问 OceanBase 官网获取更多信息:https://www.oceanbase.com/


提交一个事务会让事务的修改持久化生效,清除保存点并释放事务所持有的所有锁。

提交事务

OceanBase 数据库提交事务可以使用显式提交或者隐式提交。显式的提交事务,需要使用 COMMIT 语句或者使用提交按钮(在图形化客户端工具中)结束事务;隐式提交时不需要主动提交,当变量 autommit 值为 1 时,此时每条语句执行结束后,OceanBase 数据库将会自动的把这条语句所在的事务提交,一条语句就是一个事务。

说明

OceanBase 数据库会在 DDL 语句前和后隐式的发起一个 COMMIT 语句,也会提交事务。

  • 如果使用 BEGIN 开启一个事务,执行 DML 语句后需要使用 COMMIT 提交事务。

    • 在您提交事务之前,您的修改只对当前会话可见,对其他数据库会话是不可见的;您的修改没有持久化,所以不是最终的结果,可以用 ROLLBACK 语句回滚这些修改。

    • 在您提交事务之后,您的修改对所有数据库会话可见。您的修改结果持久化成功,不可以用 ROLLBACK 语句回滚这些修改。

    说明

    如果事务隔离级别设置了可重复读,则已经开启事务的会话无法查询到新提交的数据。事务隔离级别的更多介绍请参见 事务隔离级别

  • 如果隐式地使用事务(开启自动提交事务,即 autocommit=1),则一条 SQL 就是一个事务,不再需要提交事务。SQL 执行后,您的修改即持久化成功,不可以使用 ROLLBACK 语句回滚这些修改。

显式提交事务

通过 BEGIN 开始一个事务,然后使用 INSERT 语句在表 ordr 中插入数据,最后通过 COMMIT 语句显示提交事务。

obclient [test]> SELECT * FROM ordr;
+----+------+-------+---------------------+
| id | name | value | gmt_create          |
+----+------+-------+---------------------+
|  1 | CN   | 10001 | 2022-10-19 14:51:12 |
|  2 | US   | 10002 | 2022-10-19 14:51:12 |
|  3 | EN   | 10003 | 2022-10-19 14:51:12 |
+----+------+-------+---------------------+
3 rows in set

obclient [test]> BEGIN;
Query OK, 0 rows affected 

obclient [test]> INSERT INTO ordr(id,name) VALUES(4,'JP');
Query OK, 1 row affected 

obclient [test]> COMMIT;
Query OK, 0 rows affected 

// 执行成功后,退出会话再重新连接,发现表数据已正确插入并保存成功。

obclient [test]> exit;

$obclient -hxxx.xxx.xxx.xxx -ut***@obbmsql#obdemo -P2883 -p******  test
Welcome to the OceanBase.  Commands end with ; or \g.
Your OceanBase connection id is 3221487662
Server version: OceanBase xxx.xxx.xxx.xxx (r100000252022102910-df01cef074936b9c9f177697500fad1dc304056f) (Built Oct 29 2022 10:27:50)

Copyright (c) 2000, 2018, OceanBase and/or its affiliates. All rights reserved.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

obclient [test]> SELECT * FROM ordr;
+----+------+-------+---------------------+
| id | name | value | gmt_create          |
+----+------+-------+---------------------+
|  1 | CN   | 10001 | 2022-10-19 14:51:12 |
|  2 | US   | 10002 | 2022-10-19 14:51:12 |
|  3 | EN   | 10003 | 2022-10-19 14:51:12 |
|  4 | JP   |  NULL | 2022-10-19 14:51:44 |
+----+------+-------+---------------------+
4 rows in set

隐式提交事务

通过配置变量 autocommit=1,开启自动提交。

obclient [test]> SELECT * FROM ordr;
+----+------+-------+---------------------+
| id | name | value | gmt_create          |
+----+------+-------+---------------------+
|  1 | CN   | 10001 | 2022-10-19 14:51:12 |
|  2 | US   | 10002 | 2022-10-19 14:51:12 |
|  3 | EN   | 10003 | 2022-10-19 14:51:12 |
|  4 | JP   |  NULL | 2022-10-19 14:51:44 |
+----+------+-------+---------------------+
4 rows in set

obclient [test]> SET autocommit=1;

obclient [test]>  INSERT INTO ordr(id,name) VALUES(5,'CN');
Query OK, 1 row affected 

执行成功后,退出会话再重新连接,发现表数据已正确插入并保存成功。

obclient [test]> exit;

$obclient -hxxx.xxx.xxx.xxx -utpcc@obbmsql#obdemo -P2883 -p******  test
Welcome to the OceanBase.  Commands end with ; or \g.
Your OceanBase connection id is 3221487662
Server version: OceanBase xxx.xxx.xxx.xxx (r100000252022102910-df01cef074936b9c9f177697500fad1dc304056f) (Built Oct 29 2022 10:27:50)

Copyright (c) 2000, 2018, OceanBase Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

obclient [test]> SELECT * FROM ordr;
+----+------+-------+---------------------+
| id | name | value | gmt_create          |
+----+------+-------+---------------------+
|  1 | CN   | 10001 | 2022-10-19 14:51:12 |
|  2 | US   | 10002 | 2022-10-19 14:51:12 |
|  3 | EN   | 10003 | 2022-10-19 14:51:12 |
|  4 | JP   |  NULL | 2022-10-19 14:51:44 |
|  5 | CN   |  NULL | 2022-10-19 14:53:56 |
+----+------+-------+---------------------+
5 rows in set



欢迎访问 OceanBase 官网获取更多信息:https://www.oceanbase.com/

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论