JRuby applications in glassfish – Databases

As I promised earlier, I will now write how I added database support to a JRuby application running under glassfish v2.
The first step you’ll have to do is installing another plugin. The plugin is called “ActiveRecord-JDBC”. You can add this plugin to your rails project by adding “http://jruby-extras.rubyforge.org/svn/trunk/rails-integration/plugins/” to your plugin repositories. If you’re using gem for plugin management, just execute “gem install activerecord-jdbc”.

The second step you’ll have to do is including the Java libraries in your environment. Just edit configure/environment.rb and add “require ‘jdbc_adapter’” to it.

You’re almost done. The last step (and the important one) is to add the new database connectivity to your databases.yml. There you’ll have to edit the “production” section although  you’re developing. Add the following to your configuration:

     production:
      host: localhost
      adapter: jdbc
      driver: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost/<yourapp>_development
      username: ...
      password: ...

Just recreate your war file and put it in your autodeploy folder. Your JRuby application should now be able to access the MySQL Database.

JRuby applications in glassfish

I developed a few projects in Ruby on Rails so far. Now I noticed, that with the new glassfish v2 open source application server from sun, it is possible to load specially prepared RoR applications directly into a glassfish web container.

To develop my RoR applications I’m using the new Netbeans 6.0 IDE. They built an RoR only IDE which is available here.
To deploy a war file one needs to install a plugin for RoR to add a rake task which builds the war file. The plugin is named goldbrick. It can easaly be installed by adding “http://jruby-extras.rubyforge.org/svn/trunk/rails-integration/plugins/” to your plugin repositories.

After installing the goldbrick plugin and refreshing your list of rake tasks, you can build a war file by running rake war:standalone:create .
This will build a .war file in the projects top folder.

You can now easily deploy this application to glassfish by copying the .war file to your autodeploy folder. (eg. /opt/glassfish/domains/domain1/autodeploy).

If you now point your web browser to localhost:8080/<railsprojectname> , your rails project page should fire up.

I’m going to write soon about how to use the jdbc adapter for activerecord to access your MySQL database from within your rails application on your glassfish server.