5

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. 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.

  2. 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.

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

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

thanks

Amit Aggarwal

4

1 回答 1

9

我们需要谈谈 ACID 中的“I”(Neo4j 符合 ACID),它代表“隔离”。隔离级别告诉你,并发运行的事务看到了多少对方的数据。

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

Neo4j 在读取节点和关系时对其进行读锁(您可以获得许多读锁),在修改节点和关系时对其进行写锁。有写锁时不能获取读锁,有写锁时不能获取写锁。事务提交时释放锁。

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

为了说明,让 c 成为您的计数器值(我知道原子计数器是您最终追求的)。两个事务都将计数器加 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 所做的更新丢失。

为了防止这种情况发生,您需要在读取它们的当前值之前,通过写锁定您要显式修改的对象,将隔离级别更改为“可重复读取”。这样,它们将不会被任何其他同时运行的事务修改。

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)

希望这能让事情更清楚。

于 2013-08-28T11:30:51.717 回答