Miles to go …

September 8, 2008

TOTD #44: JDBC Connection Pooling for Rails on GlassFish v3

Filed under: glassfish, rails, totd — arungupta @ 11:30 pm

TOTD #9 explained how to configure JDBC connection pooling for Rails application deployed on GlassFish v2. There are several benefits of using using the JDBC connection pools:

  • No need to create a new database connection for each Rails instance.
  • No need to specify your password in database.yml or create a hack to hide it.
  • No garbage collection of connection after each use.

And because of the above mentioned (and other reasons) an improved application performance, scalability and efficiency.
The only way to deploy a Rails application on GlassFish v2 is to create a WAR file using Warbler. That’s a great option and is already used in production mode. GlassFish v3 takes that to the next level by allowing to deploy a Rails application without any further pacakging. This TOTD (Tip Of The Day explains how to achieve database connection pooling using GlassFish v3.

  1. Lets create a simple scaffold

    ~/samples/jruby >~/tools/jruby-1.1.3/bin/jruby -S rails jndi_rails2 -d mysql
    ~/samples/jruby/jndi_rails2 >~/tools/jruby-1.1.3/bin/jruby script/generate scaffold runner miles:float minutes:integer
    ~/samples/jruby/jndi_rails2 >~/tools/jruby-1.1.3/bin/jruby -S rake db:create
    ~/samples/jruby/jndi_rails2 >~/tools/jruby-1.1.3/bin/jruby -S rake db:migrate
  2. Install MySQL ActiveRecord JDBC adapter
    ~/samples/jruby/jndi_rails2 >~/tools/jruby-1.1.3/bin/jruby -S gem install activerecord-jdbcmysql-adapter
  3. Copy MySQL Connector/J jar to JAVA_HOME/lib/ext
    ~/samples/jruby/jndi_rails2 >sudo cp ~/tools/mysql-connector-java-5.1.6/mysql-connector-java-5.1.6-bin.jar /System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/lib/ext
  4. Start GlassFish v3 server as …
    ~/tools/glassfish/v3/b23/glassfishv3-prelude/glassfish >java -Xmx512m -DJRUBY_HOME=/Users/arungupta/tools/jruby-1.1.3 -jar modules/glassfish-10.0-SNAPSHOT.jar
  5. Setup JDBC connection pool
    1. Create JDBC connection pool

      ~/samples/jruby/jndi_rails2 >~/tools/glassfish/v3/b23/glassfishv3-prelude/glassfish/bin/asadmin create-jdbc-connection-pool –datasourceclassname com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource –restype javax.sql.DataSource –property “User=duke:Password=duke:URL=jdbc\:mysql\://localhost/jndi_rails2_production” jdbc/jndi_rails2_pool 
    2. Create JDBC resource
      ~/samples/jruby/jndi_rails2 >~/tools/glassfish/v3/b23/glassfishv3-prelude/glassfish/bin/asadmin create-jdbc-resource –connectionpoolid jdbc/jndi_rails2_pool jdbc/jndi_rails2 
    3. Ping JDBC pool
      ~/samples/jruby >~/tools/glassfish/v3/b23/glassfishv3-prelude/glassfish/bin/asadmin ping-connection-pool jdbc/jndi_rails2_pool 
  6. Change the development database in config/database.yml to:
    development:
      adapter: jdbc
      jndi: jdbc/jndi_rails2
      driver: com.mysql.jdbc.Driver 
  7. Run your app as
    ~/samples/jruby >~/tools/glassfish/v3/b22/glassfishv3-prelude/glassfish/bin/asadmin deploy jndi_rails2

And chug along with your scaffold at http://localhost:8080/jndi_rails2/runners.

    Technorati: totd rubyonrails jruby ruby glassfish v3 connectionpooling jndi jdbc mysql

    Share and Enjoy:
    • Print
    • Digg
    • Sphinn
    • del.icio.us
    • Facebook
    • Google Bookmarks
    • DZone
    • StumbleUpon
    • Technorati
    • Twitter
    • Slashdot
    Related posts:
    1. TOTD #9: Using JDBC connection pool/JNDI name from GlassFish in Rails Application
    2. TOTD #121: JDBC resource for MySQL and Oracle sample database in GlassFish v3
    3. TOTD #76: JRuby 1.2, Rails 2.3, GlassFish Gem 0.9.3, ActiveRecord JDBC Adapter 0.9.1 – can they work together ?
    4. TOTD #73: JRuby and GlassFish Integration Test #4: JRuby 1.2.0 RC2 + Rails 2.2.x + GlassFish v2 + Warbler
    5. Getting Started with Edge Rails (2.2) using JRuby and GlassFish

    11 Comments »

    1. As I understand it, Warbler spins up Rails runtimes to serve requests. Each runtime grabs a database connection as it starts and holds onto it indefinitely.

      Doesn’t that make a connection pool unnecessary for most Rails apps?

      Comment by Dick Davies — September 9, 2008 @ 1:57 am

    2. Hi Arun!
      The JDBC password in in domain.xml. It is safer than having it in database.yml?

      Comment by Alexis MP — September 9, 2008 @ 5:08 am

    3. @Alexis MP : it’s one less thing to have to worry about ’svn ignore’ing, and JNDI names make it easier to re-use the same WAR in dev. and production environments.

      Pool has several benefits to letting each runtime handle it’s own connection : montoring, setting custom properties on the driver, etc.

      I just don’t think Rails can free up connections yet – although there is built-in pooling on its way : http://ryandaigle.com/articles/2008/9/7/what-s-new-in-edge-rails-connection-pools

      Comment by Dick Davies — September 9, 2008 @ 5:37 am

    4. when rails will have build in connection pool (link in Dick Davie comment), what would be the best way? to use built in feature or to use method that is described in this post?

      Comment by Artiom — September 10, 2008 @ 12:43 am

    5. very goog!

      Comment by 一卡多号 — September 10, 2008 @ 6:07 am

    6. Alexis, Specifying db password in domain.xml is better (by being central) than being distributed as part of the config file in each application.

      Artiom, There will probably be little difference in terms of performance or functionality. JNDI connection pools might be slightly faster, and are bound to be more sophisticated. I think the answer will continue to be the same reasons you would use a JNDI pool — manageability and security (i.e. no database passwords in your application config files).

      Comment by Arun Gupta — September 10, 2008 @ 5:51 pm

    7. @Arun
      thanks

      Comment by Artiom — September 10, 2008 @ 11:59 pm

    8. [Trackback] GlassFish v3 Prelude supports deployment of Rails applications and also other dynamic language based frameworks such as Grails, which is based on groovy language.
      The support for dynamic languages based frameworks on GlassFish v3 comes through it’s mo…

      Comment by Vivek Pandey's Blog — November 6, 2008 @ 6:29 pm

    9. Is there any way without using Warbler, wouldn’t Goldspike do it as well?

      Comment by Flüge — December 3, 2008 @ 4:33 am

    10. Goldspike is the old way of doing it and now pretty much everybody either uses Warbler or you can always deploy directly on GlassFish v3 prelude as explained at:

      http://blogs.sun.com/arungupta/entry/screencast_26_develop_run_debug

      Comment by Arun Gupta — December 3, 2008 @ 5:56 am

    11. Very good

      Comment by moveis — August 9, 2009 @ 11:11 am

    RSS feed for comments on this post. TrackBack URL

    Leave a comment

    The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.
    Powered by WordPress