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

ruby – 数据库连接池

发布时间:2020-12-17 03:59:35 所属栏目:百科 来源:网络整理
导读:我在Sinatra中创建了一个小聊天应用程序,在heroku上创建了jQuery.它只是在用户提交时将消息插入数据库.并且还每2秒下载一次新消息.经过几分钟的测试后,它停止工作,我收到了一封heroku的电子邮件: Hi, We noticed that the gisekchat app had a large number
我在Sinatra中创建了一个小聊天应用程序,在heroku上创建了jQuery.它只是在用户提交时将消息插入数据库.并且还每2秒下载一次新消息.经过几分钟的测试后,它停止工作,我收到了一封heroku的电子邮件:

Hi,

We noticed that the gisekchat app had a large number of connections
open to the shared database. We had to limit the number of connections
to the shared database due to performance reasons. Can you either
reduce the number of overall connections to the shared db or move to a
dedicated database?

It does appear that you’re not taking advantage of connection pooling
and are opening a new connection to the database for each request from
your app.

Thanks,
-Chris

这是支持提交消息的动作(接收非常相似):

post '/send' do
  con = con = PGconn.connect($dbhost,5432,"",$dbname,$dbuser,$dbpass)
  con.exec("insert into messages(usr,msg,date) values('#{params[:usr]}','#{params[:msg]}',now())")    
end

我该如何更改它以启用连接池?

解决方法

是的,这是真的,每次发送’发送’后,你真的打开一个新的数据库连接.

所以你需要改变它.一种可能性是,在全球范围内打开连接:

$con = PGconn.connect($dbhost,$dbpass)

这应该在初始化$dbname …变量之后但在使用任何路由之前完成.

但是,如果您使用的是模块化的sinatra版本,而不是经典版本,则可以使用声明实例变量

attr_accessor :con

并在应用程序启动之前初始化它.

(编辑:李大同)

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

    推荐文章
      热点阅读