问题描述
我有一个Rails应用程序,现在仅在内部运行,因此现在没有太多访问.并且有两个reque worker很难运行以从Web上获取数据并将其插入到mysql数据库中,每个插入操作之后都会休眠10秒.
I have a Rails application now only runs internally, so there's not so much visits right now. And there're two resque worker running hardly to fetch data from the web and inserts into a mysql database, every insert will followed by sleep 10 second.
我们在VPS上运行它.像每隔5个小时之后,我将遇到异常Exception occured: [Mysql2::Error] closed MySQL connection"
.
We run it on a VPS. After like every 5 hours, I will encounter an Exception Exception occured: [Mysql2::Error] closed MySQL connection"
.
引起异常的原因可能是什么?现在池的大小为5.
What could be the reason causing the exception? Now the pool size is 5.
如果我增加池的大小并在database.yml
中指定reconnect: true
会有所帮助吗?
Will it help if I raise the pool size and specify reconnect: true
in my database.yml
?
推荐答案
在将mysql2 gem版本0.2.11或更低版本与多线程结合使用时,这是一个常见问题.在问题跟踪器上有一个错误,其中包含有关该问题的详细信息,但总的来说,建议要:
This is a common issue when using the mysql2 gem version 0.2.11 or below in combination with multithreading. There is a bug on the issue tracker with a details on the problem but in conclusion the advice is to:
- 将您使用的gem版本更新为
>= 0.2.12
- 在
database.yml
文件中的数据库连接配置中添加reconnect: true
选项
- Update the gem version you are using to
>= 0.2.12
- Add the
reconnect: true
option your db connection config in thedatabase.yml
file
您可能已经解决了问题,但这可能会对遇到此问题的其他人有所帮助.
You probably already solved your problem but this might help others who come across this question.
这篇关于Ruby-发生异常:[Mysql2 :: Error]关闭了MySQL连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!