gpt4 book ai didi

ruby-on-rails - Postgres : prepared statement already exists

转载 作者:数据小太阳 更新时间:2023-10-29 08:08:29 24 4
gpt4 key购买 nike

我在我的 Rails API 应用程序中使用 Devise 进行身份验证。有时我会在日志中看到以下错误:

ActiveRecord::StatementInvalid: PGError: ERROR: prepared statement "a3926" already exists: UPDATE "users" SET "current_sign_in_at" = $1, "last_sign_in_at" = $2, "sign_in_count" = $3, "updated_at" = $4 WHERE "users"."id" = 12345

错误来自设计:

  def update_tracked_fields!(request)
old_current, new_current = self.current_sign_in_at, Time.now.utc
self.last_sign_in_at = old_current || new_current
self.current_sign_in_at = new_current

old_current, new_current = self.current_sign_in_ip, request.remote_ip
self.last_sign_in_ip = old_current || new_current
self.current_sign_in_ip = new_current

self.sign_in_count ||= 0
self.sign_in_count += 1

# error happens below
save(validate: false) or raise "Devise trackable could not save #{inspect}." \
"Please make sure a model using trackable can be saved at sign in."
end

据我所知,当数据库连接的使用方式出现问题时,通常会发生此错误。有什么我应该寻找的吗?

最佳答案

如果您使用的服务器会 fork 您的进程(如 unicorn ),您需要为每个 fork 进程创建一个连接。

如果是 unicorn 添加这个:

#config/unicorn.rb
before_fork do |server, worker|

Signal.trap 'TERM' do
puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
Process.kill 'QUIT', Process.pid
end

defined?(ActiveRecord::Base) and
ActiveRecord::Base.connection.disconnect!
end

after_fork do |server, worker|

Signal.trap 'TERM' do
puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to sent QUIT'
end

defined?(ActiveRecord::Base) and
ActiveRecord::Base.establish_connection(
Rails.application.config.database_configuration[Rails.env]
)

end

关于ruby-on-rails - Postgres : prepared statement already exists,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26492059/

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