参考neo4j数据库,事务是什么意思 [英] what does transaction mean in reference with neo4j database

查看:21
本文介绍了参考neo4j数据库,事务是什么意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对术语交易有点困惑.假设在事务 A 中我们有两个命令 C1 和 C2,并且在事务 B 中相同.现在两个交易同时到来这些观察是否正确?

I got a bit confuse with term transaction. Suppose in transaction A we have two commands C1 and C2 and same in transaction B. Now both transaction come at same time then Are these observations correct?

  1. 事务 A C1 和 C2 的所有命令都会先执行(假设 A 先进入),然后只执行事务 B 的命令.

  1. All commands of transaction A C1 and C2 will be done first (assuming A enter first) , then only commands of transaction B will be executed.

事务 A 或 B 的任何命令都可以执行,但要保证如果任何事务的任何命令失败,那么该事务将被回滚.

Any command of transaction A or B can be executed but with assurance that If any of the command fails of any of the transaction then that transaction will be rollback.

如果第二种情况为真,则默认情况下在事务中,它在完成之前不会锁定任何资源

If second case is true then in transaction by default, it do not lock any resource until its completion

如果第一种情况为真,则默认情况下事务会锁定资源,直到资源完成.

If first case is true then by default transaction hold lock on resources until their completion.

谢谢

阿米特·阿加瓦尔

推荐答案

我们需要谈谈 ACID 中的I"(Neo4j 是 ACID 兼容的),它代表隔离".隔离级别告诉您,同时运行的事务看到的彼此的数据有多少.

We need to talk about the "I" in ACID (Neo4j is ACID-compliant), which stands for "isolation". The level of isolation tells you, how much of each other's data concurrently running transaction see.

Neo4j 中的默认隔离级别是已提交读".这意味着 A 不会看到 B 写入的数据,直到 B 提交.这是通过自动锁定来实现的,其工作原理如下:

The default isolation level in Neo4j is "read committed". This means A will not see the data B has written, until B commits. This is achieved by automatic locking, which works as follows:

Neo4j 读锁节点和关系(可以获得很多读锁),修改节点和关系写锁.有写锁就不能获得读锁,有写锁就不能获得写锁.事务提交时释放锁.

Neo4j read-locks nodes and relationships when you read them (you can obtain many read locks), and write-locks nodes and relationships when you modify them. Read lock can't be obtained when there is a write lock, and write lock can't be obtained when there is another write lock. Locks are released when the transaction commits.

但是,在此隔离级别可能会发生一些异常情况,其中之一称为丢失更新".

However, some anomalies can happen at this isolation level, one of which is called "lost update".

为了说明,让 c 是你的计数器值(我知道原子计数器是你最终想要的).两个事务都将计数器加 1.

For illustration, let c be your counter value (I understand an atomic counter is what your ultimately after). Both transaction are incrementing the counter by 1.

c=0
Tx1 reads c=0 (read locks c)
Tx2 reads c=0 (read locks c)
Tx1 writes c=1 (write locks c)
Tx1 commits (unlocks c)
Tx2 writes c=1 (because it thinks c is still 0, write locks c)
Tx2 commits (unlocks c)

Tx1 所做的更新丢失.

The update Tx1 has made is lost.

为了防止这种情况发生,您需要在读取当前值之前通过写锁定要预先明确修改的对象来将隔离级别更改为可重复读取".这样,它们就不会被任何其他并发运行的事务修改.

To prevent this, you need to change the isolation level to "repeatable read" by write-locking the objects you're going to modify explicitly up front, before reading their current value. This way, they will not be modifiable by any other concurrently running transaction.

c=0
Tx1 write locks c
Tx1 reads c=0 
Tx2 tries to write lock c, has to wait
Tx1 writes c=1
Tx1 commits (unlocks c)
Tx2 write locks c (because it now can)
Tx2 reads c=1 
Tx2 writes c=2 
Tx2 commits (unlocks c)

希望能让事情变得更清楚.

Hope that makes things clearer.

这篇关于参考neo4j数据库,事务是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆