Postgres:准备的声明已经存在 [英] Postgres: prepared statement already exists

查看:147
本文介绍了Postgres:准备的声明已经存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


ActiveRecord :: StatementInvalid:PGError:ERROR:prepared statement
a3926already存在:UPDATEusersSETcurrent_sign_in_at= $ 1,
last_sign_in_at= $ 2,sign_in_count= $ 3,updated_at= $ 4 WHERE
usersid= 12345


错误来自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

#错误发生在
保存(验证:false)或提高Devise trackable无法保存#{inspect}。 \
请确保在登录时可以保存使用跟踪的模型。
end

据了解,如果数据库连接出现问题,通常会发生此错误被使用。有什么我应该找的吗?

解决方案

如果您使用的是一个服务器,它需要您的流程(如独角兽)为每个分叉过程创建一个连接。



如果是独角兽,请添加以下内容:

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

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

定义?(ActiveRecord :: Base)和
ActiveRecord :: Base.connection.disconnect!
end

after_fork do | server,worker |

Signal.trap'TERM'do
put'Unicorn worker intercepting TERM并且什么都不做。等待主人发送QUIT'
end

定义?(ActiveRecord :: Base)和
ActiveRecord :: Base.establish_connection(
Rails.application.config。 database_configuration [Rails.env]


end


I use Devise for authentication in my Rails API app. 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

The error is coming out of 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

这篇关于Postgres:准备的声明已经存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆