Ruby, MySQL: Connection pool outside the rails framework?

Is there a ruby ​​stone or one for the MySQL connection pool that is not part of the rails? I just have a ruby ​​script (again, I don't do anything with rails).

+2


source to share


3 answers


A "clean database pool" should work "with any ActiveRecord application" and ActiveRecord is easy to use without Rails. I used ActiveRecord in a simple Ruby application and it was just a matter of setting up a registrar and connecting to a database, something like

ActiveRecord::Base.logger = App.logger
dbconfig = YAML::load(File.open("#{APP_ROOT}/config/database.yml"))
ActiveRecord::Base.establish_connection(dbconfig[ENV["APP_ENV"]])

      



I haven't used seamless database pooling outside of Rails, but after a quick search, I couldn't find connection pools targeting simple Ruby applications, so you might be better off.

0


source


If you are using JRuby (with an ActiveRecord adapter and JDBC), you can configure a J2EE container to manage the DB connection pool and pass the pool to database.yml via jndi, similar here (oracle example) .

Also, a few slides for MYSQL pools via ActiveRecord



ActiveRecord::Base.establish_connection(
  :adapter => 'mysql',
  :username => 'root',
  :password => '123456',
  :database => 'database',
  :pool => 5 # <- CONN POOL
)

      

0


source


You can wrap the connection_pool gem around a pair of separate connections.

Examples from doc (redis):

Usage example with block (faster):

@pool = ConnectionPool.new { Redis.new }

@pool.with do |redis|
  redis.lpop('my-list') if redis.llen('my-list') > 0
end

      

Example using replace existing connection (slower):

$redis = ConnectionPool.wrap { Redis.new }

def do_work
  $redis.lpop('my-list') if $redis.llen('my-list') > 0
end

      

0


source







All Articles