Adding JNDI DataSource support for Rails-Integration

In the past few weeks, we have been working on getting our rails application to run on JRuby. Ofcourse, we were successful in packaging our application as a J2EE .WAR archive, thanks to Rails-Integration and ARJDBC, without which this would not have been possible.

While we were at it, we also wanted to check out the jndi data source support of ARJDBC as well as Rails-Integration projects.

After spending some time, we discovered that the ARJDBC v0.2.3 had a bug in jndi_adapter.rb. The method “Jndi_Connection” was not present and instead there was a method called “Jdbc_Connection“. I had to correct the same before getting the ARJDBC to support JNDI Data source. Apparently, I also found that this issue does not exist in the trunk version of ARJDBC.

Rails-Integration comes with rake tasks to create a war file for the rails app and configure jetty
web server. To configure JNDI support, the following changes are required to be done…

1. Add the data source configuration to the jetty.xml file. However, Please note that everytime you run the rake task – war:standalone:run, the jetty.xml is recreated. Therefore, I suggest that you modify the rails-integration war plugin library, called run.rb and add the below entries in the appropriate place.

For instance, for oracle data source,

jdbc/MyApp

jdbc:oracle:thin:@192.168.2.23:1521:swami
myapp
myapp

 

For the full list of configuration details for various databases, please go here.

Please note that, as per jetty documentation, the above entries can be updated in jetty-env.xml or jetty-web.xml as well. The scope of this configuration depends on where you put this configuration information.

2. Now, search for the section

in your jetty.xml(or the run.rb file) and add the following entries after the two entries…

org.mortbay.jetty.webapp.WebInfConfiguration
org.mortbay.jetty.plus.webapp.EnvConfiguration
org.mortbay.jetty.plus.webapp.Configuration
org.mortbay.jetty.webapp.JettyWebXmlConfiguration
org.mortbay.jetty.webapp.TagLibConfiguration

3. Now, we need to specify the JNDI Logical Name for our web application, add the following entries in your web.xml under WEB-INF folder of your war file. However, Please note that every time you run the rake task – war:standalone:run, web.xml is recreated. Therefore, I suggest that you modify the rails-integration war plugin library, called create_war.rb and add the below entries in the appropriate place.

My DataSource Reference
jdbc/MyApp
javax.sql.DataSource
Container

4. Lastly, to add JNDI support, jetty requires two more libraries Jetty-Naming and Jetty-Plus.
The best way to do this is to modify the rails-integration war plugin library, called war_config.rb. Search for the word add_jetty_library and append the following lines telling the war_config to download the two libraries as well.

add_jetty_library(maven_library(‘org.mortbay.jetty’, ‘jetty-plus’, ‘6.1.1’))
add_jetty_library(maven_library(‘org.mortbay.jetty’, ‘jetty-naming’, ‘6.1.1’))

5. Now for the final step, modify your database.yml to use jndi…

production:
adapter: jndi
jndi: java:comp/env/jdbc/MyApp
driver: oracle

Thats it! Your rails web application is ready to run with JNDI Data source.

Advertisements