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

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:


      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



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

This story, "TOTD #44: JDBC Connection Pooling for Rails on GlassFish v3" was originally published by JavaWorld.

Copyright © 2008 IDG Communications, Inc.