jruby on rails and thread safety

20
JRuby on Rails and Thread Safety Naoto Takai Akasaka.rb / Itochu Techno-Solutions Corp. 29 November 2008

Post on 19-Oct-2014

8.149 views

Category:

Technology


1 download

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.2 Release Notes

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

ThreadJRuby

InstanceRails Database

One JRuby Instance,One Database Connection

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 (2)

Connection

Pool DatabaseActiveRecord

Check-out

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)

JNDI Look-up

ClientConnection

Pool Database

Look-up

DataSource

Reference

Java EE Application ServerConnection Pooling

ActiveRecord

Connection

Pool

Database

ActiveRecord

Check-out

Connection

App Server

Connection

Pool

Check-in Disconnect

Connect or

Reconnect

Connection