syntatic

Connection Pooling for Rails on JRuby using JNDI and JDBC

leave a comment »

While JRuby is getting a lot of attention for its ability to use multiple cores with a single kernel thread. There are some other performance advantages that JRuby hands you as a Rails developer from Java’s toolkit. Not only does using JDBC allow connection usage to be concurrent, but managing the database connections becomes an easier process as well.

I’ve been using JNDI (Java Naming and Directory Interface) to manage Oracle database connections for multiple warred up Rails projects deployed inside of a JBoss application server.

Setting up connection pooling is an easy process when you can find good documentation.  Hopefully this will aide the setup process.

If you aren’t using Oracle for your database (MySQL, PostgreSQL, etc) the instructions don’t change much.

First you’ll need to know where you are deploying wars inside your jboss directory (your jboss context). This is usually at <path to jboss>/server/default/deploy where ‘default’ is the context.  In the context directory we’ll need to copy the JDBC driver you’ll use into the ‘lib’ directory. I’m using Oracle so the jar will be ojdbc14.jar, ojdbc5.jar, or ojdbc6.jar. Copying the database drivers into lib means that JBoss will automatically add the jar to the classpath.

Next we’ll move into the ‘deploy’ directory off of our JBoss context.  You’ll want to create a xml datasource file.  Using Oracle my file will be named oracle-ds.xml and it looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<datasources>
  <local-tx-datasource>
    <jndi-name>DevelopmentOracleDS</jndi-name>
    <connection-url>jdbc:oracle:thin:@odev.domain.com:1523:dbdev</connection-url>
    <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
    <user-name>user</user-name>
<password>pass</password>
    <min-pool-size>0</min-pool-size>
    <max-pool-size>5</max-pool-size>
    <blocking-timeout-millis>10000</blocking-timeout-millis>
    <idle-timeout-minutes>5</idle-timeout-minutes>
    <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter</exception-sorter-class-name>
    <!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml -->
    <metadata>
       <type-mapping>Oracle9i</type-mapping>
    </metadata>
  </local-tx-datasource>
</datasources>

JBoss should automatically pick up the JNDI name and make it available.

Next we need to edit our rails database.yml file to look like this:

<% if defined?($servlet_context) %>
development:
   adapter: jdbc
   jndi: jdbc:DevelopmentOracleDS
   driver: oracle.jdbc.driver.OracleDriver
<% else %>
development:
  adapter: jdbc
  driver: oracle.jdbc.driver.OracleDriver
  url: jdbc:oracle:thin:@odev.domain.com:1523:dbde
  username: user
  password: pass
<% end %>

Because the ActiveRecord will no longer be controlling a persistent database connection you’ll need to disconnect from JNDI using the following code provided by Nick Sieger.

# config/initializers/close_connections.rb
if defined?($servlet_context)
   require 'action_controller/dispatcher'

    ActionController::Dispatcher.after_dispatch do      
      ActiveRecord::Base.clear_active_connections!
   end
end

Redeploy your rails application and now your database connections are pooled.

If you’re using GlashFish the team over at LinkedIn has setup instructions on their blog.

Written by syntatic

August 20, 2008 at 1:10 am

Posted in jruby, rails

Tagged with , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: