数据库连接池 [英] Database connection pooling

查看:147
本文介绍了数据库连接池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Sinatra和june上创建了一个小的聊天应用程序。它只是在用户提交消息时将消息插入数据库。并且每2秒下载新消息。经过几分钟的测试,它停止工作,我收到一封电子邮件形式heroku:


您好,



我们注意到gisekchat应用程序有大量的连接
打开到共享数据库。由于性能原因,我们不得不将连接数
限制到共享数据库。你可以
减少到共享数据库的总连接数,或者移动到
专用数据库?



没有利用连接池
,并为您的应用程序
中的每个请求打开一个新的数据库连接。



谢谢,
-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

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

解决方案

这是真的,你真的是打开一个新的连接数据库,每次有一个'send'post post。 p>

所以你需要改变它。一种可能性是,打开全局连接:

  $ con = PGconn.connect($ dbhost,5432, $ dbname,$ dbuser,$ dbpass)

dbname ...变量,但在使用任何路由之前。



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

  attr_accessor:con 

并在应用启动前初始化。


I created a small chat application in Sinatra and jQuery on heroku. It simply inserts a message to a database when user submits it. And also downloads new messages every 2 seconds. After a few minutes of testing it stopped working and I received an email form 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

This is the action supporting submitting a message (receiving is very similar):

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

How should I change it to enable connection pooling?

解决方案

Yeah well it's true, you really are opening a new connection to the database each time there is a 'send'-post send.

So you'll need to change that. One Possibility may be, to open the connection globally:

 $con = PGconn.connect($dbhost, 5432, "","",$dbname, $dbuser, $dbpass)

This should be done after you initialized your $dbname... variables, but before you use any routes.

However, if you are using the modular sinatra version, instead of the classic one, you could declare an instance variable with

attr_accessor :con

and initialize it before the app is started.

这篇关于数据库连接池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆