gpt4 book ai didi

eventmachine - 如何用mysql2和activerecord解决 'Mysql2::Error: This connection is still waiting for a result'错误

转载 作者:行者123 更新时间:2023-12-04 04:49:42 25 4
gpt4 key购买 nike

不重复 this question with the same title

我正在将 activerecord 与 mysql2 一起使用,并且我设计为一次处理对同一 activerecord 模型/类的 10 个查询。 请注意我使用的是严格的 activerecord 而不是直接使用 mysql 查询。

我在 Sinatra 中进行调用,然后使用 activerecord 从数据库中获取数据。

我不希望调用被阻塞所以我使用了 mysql2而且我不想使用 em-synchrony。

但现在我在后续的同时调用中收到以下“Mysql2::Error:此连接仍在等待结果,一旦获得结果再试:”。

我没有与 pool=10 建立连接

我的类(class)

  class User < ActiveRecord::Base 

和我要调用的代码 user.find(:all, :conditions => ["id=?, userid])

mysql2 文档说“要使用 ActiveRecord 驱动程序(带或不带 rails),您需要做的就是安装这个 gem 并将 database.yml 中的适配器设置为“mysql2”。这很容易吧? :)”

这正是我从 mysql 迁移到 mysql2 时所做的。

为什么会出现此错误。

最佳答案

这是一个完整的示例:

require 'rubygems'
gem 'activerecord', '~> 3.1.0'
gem 'sinatra', '~> 1.3.1'
gem 'mysql2', '~> 0.3.11'

require 'active_record'
require 'sinatra/base'
require 'mysql2'

# thin use the eventmachine thread pool
# you should have at least one connection per thread
# or you can expect errors
EM::threadpool_size = 10

# connect to the database
ActiveRecord::Base.establish_connection(
:adapter => "mysql2",
:database => "test",
:username => "root",
:encoding => 'utf8',
# number of connections openened to the database
:pool => 10
)

class App < Sinatra::Base
get '/db' do
ActiveRecord::Base.connection.execute("SELECT SLEEP(1)")
end
end

run App

要运行它,请将文件保存为“config.ru”并在线程模式下使用 thin 运行它:

thin start -e production --threaded

您可以使用 ab 来检查一切是否正常,我使用了一个名为 siege 的工具:

siege -c 10 -r 1 http://localhost:3000/db

关于eventmachine - 如何用mysql2和activerecord解决 'Mysql2::Error: This connection is still waiting for a result'错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8473551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com