Keep mysql connection open

﹥>﹥吖頭↗ 提交于 2020-01-04 12:07:24

问题


I'm making a eggdrop tcl script to write activity of several public IRC channels to a database (over time this will be 10 to 15 channels I think). I have two options how to handle the database connection in mind.

  1. An user says something -> Open a mysql connection to the database -> insert information about what the user said -> close the connection
  2. Start the bot -> Open a mysql connection to the database -> Insert information when there is channel activity -> Wait for more information etc.

I think it's better to use case 1, but when there is much channel activity I think opening and closing a connection every time will cause a massive server load and slows things down drastically after a while.

What's the best way to do this?


回答1:


If you want to keep the connection open just call

mysql::ping $dbhandle

from time to time.

This can be done with something like this:

proc keepMySqlOpen {dbhandle} {
    mysql::ping $dbhandle
    after 2000 [list keepMySqlOpen $dbhandle]
}
....
    set dbh [mysql::open ...]
    keepMySqlOpen $dbh
...

An other option is just to use mysql::ping before accessing the db, which should according to the mysqltcl manual reconnect if necessary. This might be the best of both worlds (let the connection time out if there is not much activity, keep it open otherwise).



来源:https://stackoverflow.com/questions/15161519/keep-mysql-connection-open

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!