ruby(rails) 程序如何 连接 各种远程数据库?

Python06

ruby(rails) 程序如何 连接 各种远程数据库?,第1张

在使用Cookie.find等操作的时候,就会连接到database.yml中monitor_spider配置的数据库上操作。以前一直都这么用,没发现什么不妥。最近一个项目,由于启动的进程比较多,老是碰到数据库连接池链接获取超时的错误。

通过MySQL Client用命令:show processlist发现数据库连接数量一直居高不下,轻轻松松就上2k+的连接。通过读Rails框架的connection_pool.rb文件代码,发现在各模型中用establish_connection连接数据库会造成很大的问题。文件中类ConnectionHandler的establish_connection方法代码如下:Ruby代码 def establish_connection(name, spec) @connection_pools[name] = ConnectionAdapters::ConnectionPool.new(spec) end def establish_connection(name, spec)

@connection_pools[name] = ConnectionAdapters::ConnectionPool.new(spec)

#举个例子,分段函数的处理

puts "Please input x:" #输出一些文字

x = gets #等待用户用键盘输入信息

if x<2 #以下是判断输入信息及根据输入信息进行引导

y = x+4

else

y = 2*x

end

puts "The result is:#{y}"#输出结果