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

查看:136
本文介绍了参考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.

如果第二种情况为true,则默认情况下在事务中,它不会锁定任何资源,直到其完成为止

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

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

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

谢谢

阿格加瓦尔(Amit Aggarwal)

Amit Aggarwal

推荐答案

我们需要谈谈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中的默认隔离级别为已读读".这意味着,直到B提交,A才会看到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)

希望事情变得更清楚

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

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