加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – Sinatra / ActiveRecord无法同时处理请求?

发布时间:2020-12-17 02:02:49 所属栏目:百科 来源:网络整理
导读:这是我的第一个Sinatra项目,我已经很晚了,我意识到,当使用ActiveRecord立即发出多个请求时,我会遇到问题.如果我只提出一个请求,每个请求都可以单独工作.但是,当我同时打电话给我们时,我就会失败. 到目前为止,我已经将其缩小到同时存在两个ActiveRecord请求的
这是我的第一个Sinatra项目,我已经很晚了,我意识到,当使用ActiveRecord立即发出多个请求时,我会遇到问题.如果我只提出一个请求,每个请求都可以单独工作.但是,当我同时打电话给我们时,我就会失败.

到目前为止,我已经将其缩小到同时存在两个ActiveRecord请求的问题.也许我没有正确设置ActiveRecord?我使用PostgreSQL,因为Heroku使用它,并且不会改变. (问题也发生在Heroku上.)

这是日志:

192.168.1.113 - - [30/Sep/2012:10:33:00 MDT] "GET /version/current?platform=android HTTP/1.1" 200 33
 - -> /version/current?platform=android ActiveRecord::StatementInvalid - NoMethodError: undefined method `fields' for nil:NilClass: SELECT  "rankings".* FROM "rankings"  WHERE "rankings"."user_id" = 1 LIMIT 1:
/Users/zablanc/.rvm/gems/ruby-1.9.3-head@emm/gems/activerecord-3.2.7/lib/active_record/connection_adapters/postgresql_adapter.rb:667:in `block in exec_query'

...

Warning! Rack::Session::Cookie data size exceeds 4K.
Warning! Rack::Session::Cookie failed to save session. Content dropped.

192.168.1.113 - - [30/Sep/2012:10:33:01 MDT] "GET /badges/all HTTP/1.1" 200 311
 - -> /badges/all
192.168.1.113 - - [30/Sep/2012:10:33:01 MDT] "GET /moves/ranking/all HTTP/1.1" 500 166185
 - -> /moves/ranking/all

我不知道如何关闭那些cookie警告,因为它们似乎对应用程序没有影响.以下是我配置应用程序的方法(在主脚本中需要的配置文件中):

enable :logging

use ActiveRecord::ConnectionAdapters::ConnectionManagement

use Rack::Session::Cookie,:key => 'rack.session',:path => '/',:expire_after => 31_536_000,# In seconds
                           :secret => 'jeowkfj...secret...kjn5'

ActiveRecord::Base.include_root_in_json = false

def establish_connection(url)
    db = URI.parse(url)

    ActiveRecord::Base.establish_connection(
        :adapter  => db.scheme == 'postgres' ? 'postgresql' : db.scheme,:host     => db.host,:port     => db.port,:username => db.user,:password => db.password,:database => db.path[1..-1],:encoding => 'utf8'
    )
end

configure :development do
    establish_connection('postgres://postgres:postgres@localhost:5432/emm')
end

configure :test do
    establish_connection('postgres://postgres:postgres@localhost:5432/emm-test')
end

configure :production do
    establish_connection(ENV['DATABASE_URL'])
end

我猜我没有正确设置ActiveRecord,但我认为这就像我见过的教程一样.是什么赋予了?

解决方法

听起来你正在使用线程,但在你的应用程序中有一些非线程安全的代码.

你使用的是哪个网络服务器,你使用的是哪个中间件,你使用的是哪个postgresql gem,你是否检查过你的所有宝石都是线程安全的?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读