Home > Database > Mysql Tutorial > body text

Rails使用Thread线程出现的Mysql连接数太多问题_MySQL

WBOY
Release: 2016-06-01 13:11:31
Original
1248 people have browsed it

mysql配置的最大连接数为:300

rails项目在生产环境下有两个服务器,database.yml中配置的连接池数量为:50,服务器一个跑8个unicorn进程,一个跑4个unicorn进程,总连接数能达到600。

由于对数据库的有些操作可能会影响到页面的加载速度,在项目中使用了Thread来开启一个新线程来执行一些操作,类似下面的代码:

Thread.start do    #ActiveRecord对数据库的一些操作end
Copy after login

在执行完后数据库的连接并没有关闭,导致mysql数据库连接数不断增加,超出了mysql中配置的最大连接数。

解决办法:

Thread.start do     #ActiveRecord对数据库的一些操作      if ActiveRecord::Base.connection && ActiveRecord::Base.connection.active?         ActiveRecord::Base.connection.close      end   end
Copy after login

使用ActiveRecord::Base.connection.close来手动关闭数据库连接。


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template