简体   繁体   English

Postgres:准备好的声明已经存在

[英]Postgres: prepared statement already exists

I use Devise for authentication in my Rails API app. 我在我的Rails API应用程序中使用Devise进行身份验证。 Sometimes I see following error in the logs: 有时我在日志中看到以下错误:

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 ActiveRecord :: StatementInvalid:PGError:错误:准备好的语句“a3926”已经存在:UPDATE“users”SET“current_sign_in_at”= $ 1,“last_sign_in_at”= $ 2,“sign_in_count”= $ 3,“updated_at”= $ 4 WHERE“users”。 “id”= 12345

The error is coming out of Devise: 错误来自Devise:

  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

As I understand this error usually happens when there is something wrong with how database connections are used. 据我所知,当数据库连接的使用方式出现问题时,通常会发生此错误。 Is there something I should be looking for? 有什么我应该找的吗?

If you are using a server which forks your process (like unicorn) you need to create a connection for every forked process. 如果您正在使用一个提供进程的服务器(如独角兽),则需要为每个分叉进程创建一个连接。

In case of unicorn add this: 如果是独角兽,请添加:

#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

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

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