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 http://localhost:8080/jndi_rails2/runners.

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

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?

Posted by Dick Davies on September 08, 2008 at 06:57 PM PDT #

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

Posted by Alexis MP on September 08, 2008 at 10:08 PM PDT #

@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 :

Posted by Dick Davies on September 08, 2008 at 10:37 PM PDT #

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?

Posted by Artiom on September 09, 2008 at 05:43 PM PDT #

very goog!

Posted by 一卡多号 on September 09, 2008 at 11:07 PM PDT #

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).

Posted by Arun Gupta on September 10, 2008 at 10:51 AM PDT #


Posted by Artiom on September 10, 2008 at 04:59 PM PDT #

[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...

Posted by Vivek Pandey's Blog on November 06, 2008 at 10:29 AM PST #

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

Posted by Flüge on December 02, 2008 at 08:33 PM PST #

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:

Posted by Arun Gupta on December 02, 2008 at 09:56 PM PST #

Very good

Posted by moveis on August 09, 2009 at 04:11 AM PDT #

good article

Posted by moveis moderdor on February 29, 2012 at 01:27 PM PST #

Post a Comment:
Comments are closed for this entry.

profile image
Arun Gupta is a technology enthusiast, a passionate runner, author, and a community guy who works for Oracle Corp.

Java EE 7 Samples

Stay Connected


« July 2016