gpt4 book ai didi

database - 是否可以自动释放 PostgreSQL 中的锁?

转载 作者:搜寻专家 更新时间:2023-10-30 21:55:01 26 4
gpt4 key购买 nike

我正在测试配置为 JDBC Master/Slave 的 ActiveMQ 系统的容错能力。在此设置中,有一个 postgres 数据库和两个代理——一个是主代理,另一个是从属代理。这种机制的工作方式是 master 在 db 中的表上取得排他锁。从站也尝试这样做并等待直到锁可用。如果 master 死了,应该释放锁,slave 接管。但是,如果 master 失去与数据库的网络连接,则锁永远不会释放,从而导致死锁情况。这里似乎需要一种方法来告诉 Postgres 如果在指定的时间段内没有更新则自动释放锁。 POSA 3 设计模式一书将此称为租赁模式。有可能让 Postgres 这样做吗?如果不支持,其他数据库供应商是否支持?

最佳答案

这不是死锁,这是连接丢失的问题。

当两个事务试图锁定先前被对方锁定的资源时,就会发生死锁。 PostgreSQL 检测到这些情况。

在你的例子中,master 锁定资源,slave 等待 mastermaster 等待由于连接丢失,它从未收到的用户输入。

每当 PostgreSQL 检测到连接丢失时,它会自动回滚其事务。

要控制连接丢失检测,您可以使用以下PostgreSQL connection options :

tcp_keepalives_idle (integer)

On systems that support the TCP_KEEPIDLE socket option, specifies the number of seconds between sending keepalives on an otherwise idle connection. A value of zero uses the system default. If TCP_KEEPIDLE is not supported, this parameter must be zero. This parameter is ignored for connections made via a Unix-domain socket.

tcp_keepalives_interval (integer)

On systems that support the TCP_KEEPINTVL socket option, specifies how long, in seconds, to wait for a response to a keepalive before retransmitting. A value of zero uses the system default. If TCP_KEEPINTVL is not supported, this parameter must be zero. This parameter is ignored for connections made via a Unix-domain socket.

tcp_keepalives_count (integer)

On systems that support the TCP_KEEPCNT socket option, specifies how many keepalives may be lost before the connection is considered dead. A value of zero uses the system default. If TCP_KEEPCNT is not supported, this parameter must be zero. This parameter is ignored for connections made via a Unix-domain socket.

关于database - 是否可以自动释放 PostgreSQL 中的锁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1168111/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com