JRuby on Rails and
Thread Safety
Naoto Takai
Akasaka.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

            Ruby
Process              Rails
          Instance




            Ruby
Process   ...
JRuby on Rails 2.1 or Prior

Thread

           JRuby
                     Rails
          Instance

Thread

           JR...
One JRuby Instance,
  One Database Connection




          JRuby
Thread              Rails   Database
         Instance
JRuby on Rails 2.2 or Later

Thread

              JRuby
                        Rails
             Instance

Thread

    ...
One JRuby Instance,
Many Database Connections


Thread

            JRuby
                        Rails      Database
    ...
‣ JRuby on Rails and Thread Safety
‣ ActiveRecord and Connection Pooling
‣ JNDI and Connection Pooling
ActiveRecord Connection Pool



ActiveRecord::Base.establish_connection(
  :adapter => quot;mysqlquot;,
  :username => quo...
Demo Code


10.times do
  Thread.new do
    ActiveRecord::Base.connection_pool 
                      .with_connection do ...
ActiveRecord Connection Pooling (1)


               Check-out                 Connect
                            Connect...
ActiveRecord Connection Pooling (2)


               Check-out
                            Connection
ActiveRecord        ...
ActiveRecord Connection Pooling (3)


                                         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 => quot;jdbcquot;,
  :jndi     => quot;jdbc/MySQ...
JNDI Look-up




         Look-up
                      Reference
                                  Connection
Client     ...
Java EE Application Server
                  Connection Pooling

                                            Connect or
  ...
Upcoming SlideShare
Loading in...5
×

JRuby on Rails and Thread Safety

5,341

Published on

Published in: Technology
0 Comments
7 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
5,341
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
61
Comments
0
Likes
7
Embeds 0
No embeds

No notes for slide

JRuby on Rails and Thread Safety

  1. 1. JRuby on Rails and Thread Safety Naoto Takai Akasaka.rb / Itochu Techno-Solutions Corp. 29 November 2008
  2. 2. ‣ JRuby on Rails and Thread Safety ‣ ActiveRecord and Connection Pooling ‣ JNDI and Connection Pooling
  3. 3. ‣ JRuby on Rails and Thread Safety ‣ ActiveRecord and Connection Pooling ‣ JNDI and Connection Pooling
  4. 4. Ruby on Rails 2.2 Release Notes
  5. 5. Ruby on Rails 2.1 or Prior Ruby Process Rails Instance Ruby Process Rails Database Instance Ruby Process Rails Instance
  6. 6. JRuby on Rails 2.1 or Prior Thread JRuby Rails Instance Thread JRuby Rails Database Instance Thread JRuby Rails Instance Thread
  7. 7. One JRuby Instance, One Database Connection JRuby Thread Rails Database Instance
  8. 8. JRuby on Rails 2.2 or Later Thread JRuby Rails Instance Thread JRuby Connection Rails Database Instance Pool Thread JRuby Rails Instance Thread
  9. 9. One JRuby Instance, Many Database Connections Thread JRuby Rails Database Instance Thread You need less JRuby instances per database connections.
  10. 10. ‣ JRuby on Rails and Thread Safety ‣ ActiveRecord and Connection Pooling ‣ JNDI and Connection Pooling
  11. 11. ActiveRecord Connection Pool ActiveRecord::Base.establish_connection( :adapter => quot;mysqlquot;, :username => quot;rootquot;, :database => quot;databasequot;, :pool => 5 )
  12. 12. Demo Code 10.times do Thread.new do ActiveRecord::Base.connection_pool .with_connection do |c| ret = c.execute(quot;select connection_id()quot;) puts quot;Connection ID: %squot; % ret.fetch_row end end end
  13. 13. ActiveRecord Connection Pooling (1) Check-out Connect Connection ActiveRecord Database Pool Connection
  14. 14. ActiveRecord Connection Pooling (2) Check-out Connection ActiveRecord Database Pool Connection
  15. 15. ActiveRecord Connection Pooling (3) Wait Check-out Connection ActiveRecord Database Pool Connection Check-out Check-in ActiveRecord
  16. 16. ‣ JRuby on Rails and Thread Safety ‣ ActiveRecord and Connection Pooling ‣ JNDI and Connection Pooling
  17. 17. ‣ JNDI (Java Naming and Directory Service) ‣ Binding a name to an object. ‣ Look-up the object by name.
  18. 18. Using JNDI Look-up ActiveRecord::Base.establish_connection( :adapter => quot;jdbcquot;, :jndi => quot;jdbc/MySQLquot;, :driver => quot;com.mysql.jdbc.Driverquot;, :pool => 5 )
  19. 19. JNDI Look-up Look-up Reference Connection Client Database Pool Data Source
  20. 20. Java EE Application Server Connection Pooling Connect or Check-out Reconnect ActiveRecord App Server ActiveRecord Connection Connection Pool Pool Connection Connection Check-in Disconnect Database
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×