jruby on rails and thread safety
Post on 19-Oct-2014
8.153 views
DESCRIPTION
TRANSCRIPT
JRuby on Rails andThread Safety
Naoto TakaiAkasaka.rb / Itochu Techno-Solutions Corp.29 November 2008
‣ JRuby on Rails and Thread Safety
‣ ActiveRecord and Connection Pooling
‣ JNDI and Connection Pooling
‣ JRuby on Rails and Thread Safety
‣ ActiveRecord and Connection Pooling
‣ JNDI and Connection Pooling
Ruby on Rails 2.1 or Prior
Process
Process
Process
Ruby
Instance
Ruby
Instance
Ruby
Instance
Rails
Rails
Rails
Database
JRuby on Rails 2.1 or Prior
JRuby
Instance
JRuby
Instance
JRuby
Instance
Rails
Rails
Rails
Database
Thread
Thread
Thread
Thread
JRuby on Rails 2.2 or Later
JRuby Instance
JRuby Instance
JRuby Instance
Rails
Rails
Rails
Database
Thread
Thread
Thread
Thread
ConnectionPool
One JRuby Instance,Many Database Connections
Thread
JRuby
InstanceRails Database
Thread
You need less JRuby instances per database connections.
‣ JRuby on Rails and Thread Safety
‣ ActiveRecord and Connection Pooling
‣ JNDI and Connection Pooling
ActiveRecord Connection Pool
ActiveRecord::Base.establish_connection( :adapter => "mysql", :username => "root", :database => "database", :pool => 5)
Demo Code
10.times do Thread.new do ActiveRecord::Base.connection_pool \ .with_connection do |c| ret = c.execute("select connection_id()") puts "Connection ID: %s" % ret.fetch_row end endend
ActiveRecord Connection Pooling (1)
Connection
Pool DatabaseActiveRecord
Check-out Connect
Connection
ActiveRecord Connection Pooling (3)
Connection
Pool DatabaseActiveRecord
Check-out
Connection
ActiveRecord
Check-in
Wait
Check-out
‣ JRuby on Rails and Thread Safety
‣ ActiveRecord and Connection Pooling
‣ JNDI and Connection Pooling
‣ JNDI(Java Naming and Directory Service)
‣ Binding a name to an object.
‣ Look-up the object by name.
Using JNDI Look-up
ActiveRecord::Base.establish_connection( :adapter => "jdbc", :jndi => "jdbc/MySQL", :driver => "com.mysql.jdbc.Driver", :pool => 5)