SlideShare a Scribd company logo
1 of 20
Download to read offline
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              Rails         Database
          Instance




            Ruby
Process              Rails
          Instance
JRuby on Rails 2.1 or Prior

Thread

           JRuby
                     Rails
          Instance

Thread

           JRuby
                     Rails         Database
          Instance

Thread

           JRuby
                     Rails
          Instance

Thread
One JRuby Instance,
  One Database Connection




          JRuby
Thread              Rails   Database
         Instance
JRuby on Rails 2.2 or Later

Thread

              JRuby
                        Rails
             Instance

Thread

              JRuby             Connection
                        Rails                Database
             Instance             Pool

Thread

              JRuby
                        Rails
             Instance

Thread
One JRuby Instance,
Many Database Connections


Thread

            JRuby
                        Rails      Database
           Instance

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 => quot;mysqlquot;,
  :username => quot;rootquot;,
  :database => quot;databasequot;,
  :pool     => 5
)
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
ActiveRecord Connection Pooling (1)


               Check-out                 Connect
                            Connection
ActiveRecord                                       Database
                              Pool
               Connection
ActiveRecord Connection Pooling (2)


               Check-out
                            Connection
ActiveRecord                             Database
                              Pool
               Connection
ActiveRecord Connection Pooling (3)


                                         Wait
               Check-out
                            Connection
ActiveRecord                                      Database
                              Pool
               Connection

                                      Check-out

                Check-in

                            ActiveRecord
‣ 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/MySQLquot;,
  :driver   => quot;com.mysql.jdbc.Driverquot;,
  :pool     => 5
)
JNDI Look-up




         Look-up
                      Reference
                                  Connection
Client                                         Database
                                    Pool
          Data
         Source
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

More Related Content

What's hot

Using Node.js to Build Great Streaming Services - HTML5 Dev Conf
Using Node.js to  Build Great  Streaming Services - HTML5 Dev ConfUsing Node.js to  Build Great  Streaming Services - HTML5 Dev Conf
Using Node.js to Build Great Streaming Services - HTML5 Dev Conf
Tom Croucher
 
Node js presentation
Node js presentationNode js presentation
Node js presentation
martincabrera
 

What's hot (20)

Using Node.js to Build Great Streaming Services - HTML5 Dev Conf
Using Node.js to  Build Great  Streaming Services - HTML5 Dev ConfUsing Node.js to  Build Great  Streaming Services - HTML5 Dev Conf
Using Node.js to Build Great Streaming Services - HTML5 Dev Conf
 
EPAM IT WEEK: AEM & TDD. It's so boring...
EPAM IT WEEK: AEM & TDD. It's so boring...EPAM IT WEEK: AEM & TDD. It's so boring...
EPAM IT WEEK: AEM & TDD. It's so boring...
 
Elastic search 클러스터관리
Elastic search 클러스터관리Elastic search 클러스터관리
Elastic search 클러스터관리
 
Introduction to reactive programming & ReactiveCocoa
Introduction to reactive programming & ReactiveCocoaIntroduction to reactive programming & ReactiveCocoa
Introduction to reactive programming & ReactiveCocoa
 
Django REST Framework
Django REST FrameworkDjango REST Framework
Django REST Framework
 
[Srijan Wednesday Webinar] Rails 5: What's in It for Me?
[Srijan Wednesday Webinar] Rails 5: What's in It for Me?[Srijan Wednesday Webinar] Rails 5: What's in It for Me?
[Srijan Wednesday Webinar] Rails 5: What's in It for Me?
 
Celery with python
Celery with pythonCelery with python
Celery with python
 
Celery - A Distributed Task Queue
Celery - A Distributed Task QueueCelery - A Distributed Task Queue
Celery - A Distributed Task Queue
 
Everything as a code
Everything as a codeEverything as a code
Everything as a code
 
Django Celery
Django Celery Django Celery
Django Celery
 
Spring & Hibernate
Spring & HibernateSpring & Hibernate
Spring & Hibernate
 
Gradle Introduction
Gradle IntroductionGradle Introduction
Gradle Introduction
 
Building your first Node app with Connect & Express
Building your first Node app with Connect & ExpressBuilding your first Node app with Connect & Express
Building your first Node app with Connect & Express
 
RESTful API In Node Js using Express
RESTful API In Node Js using Express RESTful API In Node Js using Express
RESTful API In Node Js using Express
 
Scaling up task processing with Celery
Scaling up task processing with CeleryScaling up task processing with Celery
Scaling up task processing with Celery
 
Connection Pooling
Connection PoolingConnection Pooling
Connection Pooling
 
Node.js
Node.jsNode.js
Node.js
 
Node js presentation
Node js presentationNode js presentation
Node js presentation
 
Dmp hadoop getting_start
Dmp hadoop getting_startDmp hadoop getting_start
Dmp hadoop getting_start
 
NodeJs
NodeJsNodeJs
NodeJs
 

Similar to JRuby on Rails and Thread Safety

Spring db-access mod03
Spring db-access mod03Spring db-access mod03
Spring db-access mod03
Guo Albert
 
Admin High Availability
Admin High AvailabilityAdmin High Availability
Admin High Availability
rsnarayanan
 
MongoDB for Java Devs with Spring Data - MongoPhilly 2011
MongoDB for Java Devs with Spring Data - MongoPhilly 2011MongoDB for Java Devs with Spring Data - MongoPhilly 2011
MongoDB for Java Devs with Spring Data - MongoPhilly 2011
MongoDB
 

Similar to JRuby on Rails and Thread Safety (20)

Rollin onj Rubyv3
Rollin onj Rubyv3Rollin onj Rubyv3
Rollin onj Rubyv3
 
Jdbc
JdbcJdbc
Jdbc
 
DeveloperWeek Europe 2023 - Revolutionize Java DB AppDev with Reactive Stream...
DeveloperWeek Europe 2023 - Revolutionize Java DB AppDev with Reactive Stream...DeveloperWeek Europe 2023 - Revolutionize Java DB AppDev with Reactive Stream...
DeveloperWeek Europe 2023 - Revolutionize Java DB AppDev with Reactive Stream...
 
Complete java
Complete javaComplete java
Complete java
 
TDC Connections 2023 - Revolutionize Java DB AppDev with Reactive Streams and...
TDC Connections 2023 - Revolutionize Java DB AppDev with Reactive Streams and...TDC Connections 2023 - Revolutionize Java DB AppDev with Reactive Streams and...
TDC Connections 2023 - Revolutionize Java DB AppDev with Reactive Streams and...
 
04.egovFrame Runtime Environment Workshop
04.egovFrame Runtime Environment Workshop04.egovFrame Runtime Environment Workshop
04.egovFrame Runtime Environment Workshop
 
Connecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRubyConnecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRuby
 
JRuby - Programmer's Best Friend on JVM
JRuby - Programmer's Best Friend on JVMJRuby - Programmer's Best Friend on JVM
JRuby - Programmer's Best Friend on JVM
 
The Java EE 7 Platform: Productivity++ & Embracing HTML5
The Java EE 7 Platform: Productivity++ & Embracing HTML5The Java EE 7 Platform: Productivity++ & Embracing HTML5
The Java EE 7 Platform: Productivity++ & Embracing HTML5
 
jPrime 2023 - Revolutionize Java DB AppDev with Reactive Streams and Virtual ...
jPrime 2023 - Revolutionize Java DB AppDev with Reactive Streams and Virtual ...jPrime 2023 - Revolutionize Java DB AppDev with Reactive Streams and Virtual ...
jPrime 2023 - Revolutionize Java DB AppDev with Reactive Streams and Virtual ...
 
The Java EE 7 Platform: Productivity & HTML5 at San Francisco JUG
The Java EE 7 Platform: Productivity & HTML5 at San Francisco JUGThe Java EE 7 Platform: Productivity & HTML5 at San Francisco JUG
The Java EE 7 Platform: Productivity & HTML5 at San Francisco JUG
 
Jdbc
JdbcJdbc
Jdbc
 
Data Integration with server side Mashups
Data Integration with server side MashupsData Integration with server side Mashups
Data Integration with server side Mashups
 
Spring db-access mod03
Spring db-access mod03Spring db-access mod03
Spring db-access mod03
 
JDBC
JDBCJDBC
JDBC
 
Admin High Availability
Admin High AvailabilityAdmin High Availability
Admin High Availability
 
Clojure at BackType
Clojure at BackTypeClojure at BackType
Clojure at BackType
 
Quick Intro To JRuby
Quick Intro To JRubyQuick Intro To JRuby
Quick Intro To JRuby
 
10 J D B C
10  J D B C10  J D B C
10 J D B C
 
MongoDB for Java Devs with Spring Data - MongoPhilly 2011
MongoDB for Java Devs with Spring Data - MongoPhilly 2011MongoDB for Java Devs with Spring Data - MongoPhilly 2011
MongoDB for Java Devs with Spring Data - MongoPhilly 2011
 

More from Naoto Takai (6)

私と自分戦略
私と自分戦略私と自分戦略
私と自分戦略
 
地域.rbのご提案
地域.rbのご提案地域.rbのご提案
地域.rbのご提案
 
JRuby最新事情@札幌
JRuby最新事情@札幌JRuby最新事情@札幌
JRuby最新事情@札幌
 
JRoRでGAE/Jの手引き
JRoRでGAE/Jの手引きJRoRでGAE/Jの手引き
JRoRでGAE/Jの手引き
 
『エンタープライズRails』に学ぶ企業ユーザのためのRailsアーキテクチャ
『エンタープライズRails』に学ぶ企業ユーザのためのRailsアーキテクチャ『エンタープライズRails』に学ぶ企業ユーザのためのRailsアーキテクチャ
『エンタープライズRails』に学ぶ企業ユーザのためのRailsアーキテクチャ
 
5分でわかるJRuby最新事情
5分でわかるJRuby最新事情5分でわかるJRuby最新事情
5分でわかるJRuby最新事情
 

Recently uploaded

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 

Recently uploaded (20)

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 

JRuby on Rails and Thread Safety

  • 1. JRuby on Rails and Thread Safety Naoto Takai Akasaka.rb / Itochu Techno-Solutions Corp. 29 November 2008
  • 2. ‣ JRuby on Rails and Thread Safety ‣ ActiveRecord and Connection Pooling ‣ JNDI and Connection Pooling
  • 3. ‣ JRuby on Rails and Thread Safety ‣ ActiveRecord and Connection Pooling ‣ JNDI and Connection Pooling
  • 4. Ruby on Rails 2.2 Release Notes
  • 5. Ruby on Rails 2.1 or Prior Ruby Process Rails Instance Ruby Process Rails Database Instance Ruby Process Rails Instance
  • 6. JRuby on Rails 2.1 or Prior Thread JRuby Rails Instance Thread JRuby Rails Database Instance Thread JRuby Rails Instance Thread
  • 7. One JRuby Instance, One Database Connection JRuby Thread Rails Database Instance
  • 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. One JRuby Instance, Many Database Connections Thread JRuby Rails Database Instance Thread You need less JRuby instances per database connections.
  • 10. ‣ JRuby on Rails and Thread Safety ‣ ActiveRecord and Connection Pooling ‣ JNDI and Connection Pooling
  • 11. ActiveRecord Connection Pool ActiveRecord::Base.establish_connection( :adapter => quot;mysqlquot;, :username => quot;rootquot;, :database => quot;databasequot;, :pool => 5 )
  • 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. ActiveRecord Connection Pooling (1) Check-out Connect Connection ActiveRecord Database Pool Connection
  • 14. ActiveRecord Connection Pooling (2) Check-out Connection ActiveRecord Database Pool Connection
  • 15. ActiveRecord Connection Pooling (3) Wait Check-out Connection ActiveRecord Database Pool Connection Check-out Check-in ActiveRecord
  • 16. ‣ JRuby on Rails and Thread Safety ‣ ActiveRecord and Connection Pooling ‣ JNDI and Connection Pooling
  • 17. ‣ JNDI (Java Naming and Directory Service) ‣ Binding a name to an object. ‣ Look-up the object by name.
  • 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. JNDI Look-up Look-up Reference Connection Client Database Pool Data Source
  • 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