简体   繁体   English

锁表直到事务提交

[英]lock table till the transaction commit

I have two tables A and B related to ledger. 我有两张与分类帐相关的表A和B. Many transactions happen in both A and B parallely. 许多交易平行地发生在A和B中。

Problem faced is: 面临的问题是:

[28] - A insert success for source entity_id: id001
[28] - B update success for destination entity_id: id002
[28] - A insert success for destination entity_id: id002
[71] - B update success for source entity_id: id001                  
[28] - commit success for [28] with amount 100

Thread 71 updates table B before thread 28 commits the changes to the ledger. 线程71在线程28将更改提交到分类帐之前更新表B.

It is happening because postgres locks the table when a process is using it. 它正在发生,因为postgres在进程使用它时锁定表。

  1. How can i make thread to wait till the commit is completed by current thread? 如何使线程等到当前线程完成提交?
  2. How to lock a record in postgres? 如何锁定postgres中的记录? (so that other accounts in table can be accessed by users) (以便用户可以访问表中的其他帐户)

Postgres locks the record by default. Postgres默认锁定记录。 Refer docs for the same. 请参阅相同的文档

For the logs provided, the last log must be after committing the transaction. 对于提供的日志,最后一个日志必须在提交事务之后。 Whenever the transaction is complete, the waiting thread immediately takes over and that must be the reason your logs showed up that way. 每当事务完成时,等待的线程立即接管,这必然是您的日志以这种方式出现的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM