Skip to content

Mysql2::Error This connection is in use #825

@Muzicius

Description

@Muzicius

Hello. I have a sinatra application which provides REST API for my web app. I'm facing this problem from time to time randomly.
image_2017-02-05_05-22-22

I use ruby 2.3.1 and mysql2 0.4.4. Here is my code of connection. I use Singleton in order to create only one connection to database.

require 'mysql2'
require 'singleton'
require_relative 'config'

class DatabaseHandler
  include Singleton
  attr_reader :connection

  def self.connection
    instance.connection
  end

  def self.setup
    instance.setup
  end

  def setup
    @connection = Mysql2::Client.new(:host => MYSQL_DB_HOST, :port => MYSQL_DB_PORT, :username => MYSQL_DB_USER, :password => MYSQL_DB_PASSWORD, :database => MYSQL_DB_NAME, :reconnect => true)
    @connection.query_options.merge!(:symbolize_keys => true)
  end
end

I have tried to find a solution, but all of them were for Rails and ActiveRecord. What am I doing wrong?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions