Postgresql 事务总结

2019-10-07  本文已影响0人  林晨chris

Postgresql 事务总结

五种并发问题

如何理解事务呢?

举个例子, 从A转100元给B, 需要2条sql,

update account set balance = balance - 100 where name = 'A'
update account set balance = balance + 100 where name = 'B'

如何加事务呢?

default, every single sql is a transaction, we also can use "begin" and "commit" to specific a transaction

BEGIN;
update account set balance = balance - 100 where name = 'A'
update account set balance = balance + 100 where name = 'B'
commit;

更细粒度可以用 savepoint 和rollback to 来控制.

BEGIN;
update account set balance = balance - 100 where name = 'A'
savepoint update_A_success;
update account set balance = balance + 100 where name = 'B'
rollback to update_A_success;
commit;

Demo

成功之后, 现在我们在当前会话里可以查到alice, 但在其它会话里查不到.

current session
other session

直到我们commit, 其它会话才可以看到.

上一篇下一篇

猜你喜欢

热点阅读