2012-09-30 18 views
4

這是我的第一個項目西納特拉,我敢下旬,我意識到,在做一旦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/[email protected]/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:[email protected]:5432/emm') 
end 

configure :test do 
    establish_connection('postgres://postgres:[email protected]:5432/emm-test') 
end 

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

我猜我不設立ActiveRecord的權利,但我覺得它就像我以前做的教程看到。是什麼賦予了?

回答

1

聽起來你正在使用的線程,但在你的應用程序的一些非線程安全的代碼。

哪些是你使用,你使用它的中間件,您使用的這些寶石PostgreSQL的Web服務器,你檢查一下你的所有寶石是線程安全的?

相關問題