简体   繁体   中英

Ruby, MySQL: Connection pool that is outside of the rails framework?

Is there a ruby gem or such for MySQL connection pooling that isn't part of rails? I simply have a ruby script (again, I don't do anything with rails).

Seamless Database Pool is supposed to work "with any ActiveRecord application", and ActiveRecord is easy to use without Rails. I've used ActiveRecord in a plain Ruby app, and it was just a matter of configuring the logger and the database connection, 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 Pool outside of Rails, but I couldn't find any connection poolers aimed at plain Ruby apps after a quick search so it might be your best bet.

If you use JRuby (with ActiveRecord and JDBC adapter) you can configure a J2EE container to handle DB connection pool and pass the pool in database.yml via jndi similar to here (example with oracle) .

Also, some slides for MYSQL pools via ActiveRecord

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

You could wrap the connection_pool gem around a couple single connections.

Examples from the doc (redis):

Example usage with block (faster):

 @pool = ConnectionPool.new { Redis.new } @pool.with do |redis| redis.lpop('my-list') if redis.llen('my-list') > 0 end 

Example usage replacing an existing connection (slower):

 $redis = ConnectionPool.wrap { Redis.new } def do_work $redis.lpop('my-list') if $redis.llen('my-list') > 0 end 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM