开发者

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

开发者 https://www.devze.com 2022-12-26 03:18 出处:网络
Is there a ruby gem or such for MySQL connection pooling that isn\'t part of rails? I simply have a ruby scrip开发者_如何学JAVAt (again, I don\'t do anything with rails).Seamless Database Pool is supp

Is there a ruby gem or such for MySQL connection pooling that isn't part of rails? I simply have a ruby scrip开发者_如何学JAVAt (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
0

精彩评论

暂无评论...
验证码 换一张
取 消