ActiveRecord-JDBC 0.6 Released!

Posted by Nick Sieger Tue, 06 Nov 2007 15:00:00 GMT

Just out is ActiveRecord-JDBC 0.6, the post-RubyConf release.

The sparkly new feature is Rails 2.0 support. In the soon-to-be-released Rails 2.0 (edge), Rails will automatically look for and load an adapter gem based on the name of the adapter you specify in database.yml. Example:

development:
  adapter: funkdb
  ...

With this database configuration, Rails will attempt to load the activerecord-funkdb-adapter gem, require the active_record/connection_adapters/funkdb_adapter library, and call the method ActiveRecord::Base.funkdb_connection in order to obtain a connection to the database. (This is the mechanism used to off-load non-core adapters out of the Rails codebase.)

We can leverage this convention to make it easier than ever to get started using JRuby with your Rails application. So, the first thing new in the 0.6 release is the name. You now install activerecord-jdbc-adapter:

jruby -S gem install activerecord-jdbc-adapter

But wait, there’s more! We also have adapters for four open-source databases, including MySQL, PostgreSQL, and two embedded Java databases, Derby and HSQLDB. And, for your convenience, we’ve bundled the JDBC drivers in dependent gems, so you don’t have to go hunting them down if you don’t have them handy.

Check this out. Get a fresh copy of JRuby 1.0.2, unpack it, and add the bin directory to your path. Install the adapter:

$ jruby -S gem install activerecord-jdbcderby-adapter --include-dependencies
Successfully installed activerecord-jdbcderby-adapter-0.6
Successfully installed activerecord-jdbc-adapter-0.6
Successfully installed jdbc-derby-10.2.2.0

In your Rails application, freeze to edge Rails (soon to be Rails 2.0).

rake rails:freeze:edge

Re-run the Rails command, regenerating configuration files.

jruby ./vendor/rails/railties/bin/rails .

Currently, Rails 2.0 uses openssl for the HMAC digest used in the new cookie session store, so we have to install the jruby-openssl gem:

jruby -S gem install jruby-openssl

Now, update your config/database.yml as follows:

development:
  adapter: jdbcderby
  database: db/development

Re-run your migrations, and you should now see a Derby database footprint in the db/development directory.

$ ls -l db/development
total 24
-rw-r--r--    1 nicksieg  nicksieg    38 Nov  6 08:24 db.lck
-rw-r--r--    1 nicksieg  nicksieg     4 Nov  6 08:24 dbex.lck
drwxr-xr-x    5 nicksieg  nicksieg   170 Nov  6 08:24 log/
drwxr-xr-x   65 nicksieg  nicksieg  2210 Nov  6 08:24 seg0/
-rw-r--r--    1 nicksieg  nicksieg   882 Nov  6 08:24 service.properties
drwxr-xr-x    2 nicksieg  nicksieg    68 Nov  6 08:24 tmp/

That’s it! To re-emphasize, to make your application run under JRuby, no longer will you need to a) find and download appropriate JDBC drivers, b) wonder where they should be placed so that JRuby will find them, or c) make custom changes to config/environment.rb. All that’s taken care of you if you use one of the following adapters:

  • activerecord-jdbcmysql-adapter (MySQL)
  • activerecord-jdbcpostgresql-adapter (PostgreSQL)
  • activerecord-jdbcderby-adapter (Derby)
  • activerecord-jdbchsqldb-adapter (HSQLDB)

If you need to connect to a different database, you’ll still need to place your database’s JDBC driver jar file in the appropriate place and use the straight activerecord-jdbc-adapter. Also note that in this case, and for Rails 1.2.x in general, you’ll still need to add that pesky require statement to config/environment.rb.

As always, there are bug fixes too (though we haven’t been tracking exactly which ones are fixed). We’re starting to file ActiveRecord-JDBC bugs in the JRuby JIRA now, and will be putting in future AR-JDBC versions to target soon too. So, please file new bugs in JIRA (and select component “ActiveRecord-JDBC”) rather than in the antiquated Rubyforge tracker.

Tags , ,  | 9 comments

Comments

  1. Avatar labria said about 2 hours later:

    Wow. That’s a good one!

  2. Avatar Dick Davies said about 10 hours later:

    Does this onry work with Rails 2.0?

    The classpath/jar issue is a big PITA when trying to get existing apps to run on JRuby too.

  3. Avatar Matthew Williams said about 23 hours later:

    Syntax should be: jruby -S gem install activerecord-jdbc-adapter

    (capital S versus lower-case)

    Great write up!

  4. Avatar Nick said 1 day later:

    @Dick: I think this approach should work fine with Rails < 2.0. The only real difference is that Rails won’t auto-load the gem for you, so you’ll have to put some glue into config/environment.rb in the usual spot above the Rails::Initializer. Example:

    require 'rubygems'
    gem 'activerecord-jdbcmysql-adapter'
    

    @Matthew: typo noted, thanks.

  5. Avatar Oliver Schmelzle said 1 day later:

    Thanks for releasing this new version.

    When using Radiant 0.6.3 with ActiveRecord-JDBC 0.6 I noticed migrations were failing due to a problem with renaming column names.

    I filed a bug and fix for this issue at:

    http://jira.codehaus.org/browse/JRUBY-1543

  6. Avatar Francois Orsini said 1 day later:

    This is great stuffs Nick. Forgive my naive question but if I want to connect to a remote Derby instance (not an embedded one), I would still need to use the straight activerecord-jdbc-adapter and set the appropriate settings (driver class & connection URL) as well as placing the derbyclient.jar in that case in some appropriate location... The activerecord-jdbcderby-adapter is only to connect to a embedded Derby instance, right? Cheers.

  7. Avatar Nick said 1 day later:

    @François: yes, unfortunately we have the embedded derby URL hard-coded in the jdbc_derby.rb file in the activerecord-jdbc-adapter codebase, so you’ll have to use the url/driver properties in your database.yml. But you still should be able to use adapter == ‘jdbcderby’.

  8. Avatar Tom said 12 days later:

    How do you configure it for Oracle? There is no activerecord-oracle-adapter gem.

  9. Avatar Nick said 13 days later:

    @Tom: Yes, that’s correct, because Oracle’s driver is proprietary. You use the activerecord-jdbc-adapter gem only, with the standard configuration. See the documentation for details.