Orientdb 简明教程
OrientDB - Transactions
与 RDBMS 类似,OrientDB 支持事务 ACID 属性。 transaction 包含在数据库管理系统内执行的工作单元。在数据库环境中维护事务有两个主要原因。
Like RDBMS, OrientDB supports transactions ACID properties. A transaction comprises a unit of work performed within a database management system. There are two main reasons to maintain transactions in a database environment.
-
To allow concurrent recovery from failures and keep a database consistent even in case of system failures.
-
To provide isolation between programs accessing a database concurrently.
默认情况下,数据库事务必须遵循 ACID 属性,如原子性、一致性、隔离性和持久性属性。但 OrientDB 是一个 ACID 兼容数据库,这意味着它不会矛盾或否定 ACID 的概念,而是在处理 NoSQL 数据库时改变了它的认知。了解 ACID 属性如何与 NoSQL 数据库一起工作。
By default, the database transaction must follow ACID properties such as Atomic, Consistent, Isolated, and Durable properties. But OrientDB is an ACID compliant database, which means it does not contradict or negate the concept ACID, but it changes its perception while handling the NoSQL database. Take a look at how ACID properties work along with NoSQL database.
Atomic −当您执行更改数据库操作时,更改应完全成功或失败。
Atomic − When you do something to change the database the change should work or fail as a whole.
Consistent −数据库应保持一致。
Consistent − The database should remain consistent.
Isolated −如果其他事务执行同时执行,那么用户将无法看到并行执行中的记录。
Isolated − If other transaction executions are executing at the same time, then the user will not be able to see the records in concurrent execution.
Durable −如果系统崩溃(硬件或软件),数据库本身应该能够进行备份。
Durable − If the system crashes (hardware or software), the database itself should be able to take a backup.
可以使用提交和回滚命令实现数据库事务。
Database transaction can be achieved by using Commit and Rollback commands.
Commit
提交表示通过将所有更改保存到数据库来关闭事务。回滚表示将数据库状态恢复到打开事务时的点。
Commit means closing the transaction by saving all changes to the database. Rollback means recover the database state to the point where you opened the transaction.
以下是 COMMIT 数据库命令的基本语法。
The following statement is the basic syntax of the COMMIT database command.
COMMIT
Note −您必须在连接到特定的数据库并且开始事务之后才能使用此命令。
Note − You can use this command only after connecting to a particular database and after beginning the transaction.
Example
在这个示例中,我们将使用这个教程早期某个章节中创建的、称为“demo”的相同的数据库。我们将看到执行事务的操作并将记录使用事务存储起来。
In this example, we will use the same database named ‘demo’ that we created in an earlier chapter of this tutorial. We will see the operation of commit transaction and store a record using transactions.
您需要首先使用以下 BEGIN 命令开始事务。
You need to first start the transaction using the following BEGIN command.
orientdb {db = demo}> BEGIN
使用以下命令向包含 id = 12 和 name = satish.P 值的 employee 表中插入一条记录。
Insert a record into an employee table with the values id = 12 and name = satish.P using the following command.
orientdb> INSERT INTO employee (id, name) VALUES (12, 'satish.P')
您可以使用以下命令执行事务。
You can use the following command to commit the transaction.
orientdb> commit
如果此事务成功执行,您将得到以下输出。
If this transaction successfully committed, you will get the following output.
Transaction 2 has been committed in 4ms
Rollback
回滚意味着将数据库状态恢复到打开事务时的点。
Rollback means recovering the database state to the point where you opened the transaction.
以下语句是 ROLLBACK 数据库命令的基本语法。
The following statement is the basic syntax of the ROLLBACK database command.
ROLLBACK
Note −您必须在连接到特定的数据库并且开始事务之后才能使用此命令。
Note − You can use this command only after connecting to a particular database and after beginning the transaction.
Example
在这个示例中,我们将使用这个教程早期某个章节中创建的、称为“demo”的相同的数据库。我们将看到执行回滚事务的操作并将记录使用事务存储起来。
In this example, we will use the same database named ‘demo’ that we created in an earlier chapter of the tutorial. We will see the operation of rollback transaction and store a record using transactions.
您必须首先使用以下 BEGIN 命令开始事务。
You have to first start the transaction using the following BEGIN command.
orientdb {db = demo}> BEGIN
使用以下命令向包含 id = 12 和 name = satish.P 值的 employee 表中插入一条记录。
Insert a record into an employee table with the values id = 12 and name = satish.P using the following command.
orientdb> INSERT INTO employee (id, name) VALUES (12, 'satish.P')
您可以使用以下命令检索表 employee 的记录。
You can use the following command to retrieve the records of the table employee.
orientdb> SELECT FROM employee WHERE name LIKE '%.P'
如果成功执行此命令,您将得到以下输出。
If this command is executed successfully, you will get the following output.
---+-------+--------------------
# | ID | name
---+-------+--------------------
0 | 12 | satish.P
---+-------+--------------------
1 item(s) found. Query executed in 0.076 sec(s).
您可以使用以下命令回滚此事务。
You can use the following command to Rollback this transaction.
orientdb> ROLLBACK
再次查看 select 查询来从 Employee 表中检索相同的记录。
Check the select query again to retrieve the same record from the Employee table.
orientdb> SELECT FROM employee WHERE name LIKE '%.P'
如果 Rollback 成功执行,您将在输出中得到 0 条记录已找到。
If the Rollback is executed successfully, you will get 0 records found in the output.
0 item(s) found. Query executed in 0.037 sec(s).