2014-12-24 41 views
0

我正在使用單個PostgreSQL數據庫作爲其數據的Rails應用程序。但是,在應用程序的某些部分,我使用WordNet - 一個英語NLP數據庫。由於NLP分析消耗了大量資源,因此我將它放在不同的服務器上,放在單獨的PostgreSQL實例中,這樣它就不會消耗我的Web服務器資源。對於未來,我也在考慮擴展這個數據庫,但這是一個不同的故事。無論如何,通過這種配置,我有兩臺服務器和兩個數據庫:Rails用於其模型的數據庫和第二個數據庫 - 包含大量數據的NLP數據庫。Rails應用程序中的自定義數據庫連接

我的問題是:在Rails應用程序中確切地說應該與NLP數據庫建立連接嗎?目前,我已經把它在我的配置/環境/ * RB文件,它看起來像這樣:

config.wordnet_connection = PG::Connection.new(host:'hostname',dbname:'dbname',user:'username',password:'password') 

所以現在,在控制器中,當我需要訪問數據庫NLP我使用此代碼:

conn = Rails.application.config.wordnet_connection 
conn.exec(sql) 

然而,當我部署的應用程序,以我的生產服務器(使用nginx的+ PHUSION乘客)有時我得到它試圖從wordnet_connection得到一些數據控制器的錯誤消息:

PG :: Contro中的UnableToSend米勒#動作

服務器

以下行發生這個錯誤沒有關係:

res = Rails.application.config.wordnet_connection.exec(sql) 

我在做什麼錯?

回答

0

保持配置在一個地方database.yml

production: 
    adapter: postgresql 
    other stuff... 

wordnet_connection: 
    adapter: postgresql 
    other stuff.. 

other_envs: 
..... 

然後創建一個類

class WordnetConnection < ActiveRecord::Base 
    establish_connection(:wordnet_connection) 
    self.table_name = "your_table" 
end 

然後你就可以訪問就像

WordnetConnection.first 

我覺得這樣下去備用數據庫連接打開這可能是一件好事對於性能也可以讓你使用AR

查看我的博客http://imnithin.weebly.com/multiple-dbs-in-rails.html

相關問題