Skip to content

ActiveRecord JDBC

daidoji edited this page Apr 30, 2011 · 6 revisions

» JRuby Project Wiki Home Page

ActiveRecord-JDBC Database Support

ActiveRecord-JDBC 0.9.1 supports normal AR operations and basic migrations for the following databases:
  • MySQL
  • PostgreSQL
  • SQLite3 (except change_column)
  • Oracle
  • HSQLDB (except migrations and habtm at least.)
  • H2
  • Microsoft SQL Server (except for change_column_default)
  • DB2 (except change_column, change_column_default, rename_column, remove_column, add_index, remove_index, and rename_table)
  • Derby (except change_column, change_column_default, remove_column, rename_column)
  • FireBird (except change_column_default and rename_column)

Usage

Here's a walkthrough for MySQL in ultra-condensed form:

1. Grab a JRuby release from JRuby Downloads and unpack it

2. Set up path for JRuby

      export PATH=$PATH:[jruby-dir]/bin

3. Install Rails

      jruby -S gem install rails

4. Install ActiveRecord-JDBC for MySQL (see the documentation for other options). This will also install the dependent gems.

      jruby -S gem install activerecord-jdbcmysql-adapter
You should see something like the following:
      Successfully installed activerecord-jdbc-adapter-0.9.2
      Successfully installed jdbc-mysql-5.0.4
      Successfully installed activerecord-jdbcmysql-adapter-0.9.2

5. Generate a Rails App

      rails ~/testapp --database mysql

(In rails v3 this is now)

      rails new ~/testapp --database mysql

6. Go to the app

      cd ~/testapp

7. If you're using Rails 2.0 modify database.yml by prepending jdbc to the adapter name. For PostgreSQL, you'll need to add the host parameter as well. If you're NOT using Rails 2.0, don't modify your database.yml file.

ActiveRecord-JDBC version 0.9.0:

      #SQLite3
      development:
        adapter: jdbcsqlite3
        url: jdbc:sqlite:test.db # path to sqlite3 dbfile

ActiveRecord-JDBC version 0.9.1:

      #SQLite3
      development:
        adapter: jdbcsqlite3
        database: db/development.db
      #MYSQL
      development:
        adapter: jdbcmysql
        encoding: utf8
        database: testapp_development
        username: root
        password:
      #POSTGRES
      development:
        adapter: jdbcpostgresql
        encoding: unicode
        host: localhost
        database: testapp_development
        username: testapp
        password:
      #ORACLE
      development:
        adapter: jdbc
        driver: oracle.jdbc.OracleDriver
        url: jdbc:oracle:thin:@myOracleHost:1521:mySID
        username: myUser
        password: myPass



8. If you're using Rails 2.0, you're done. The rest of configuration, including the JDBC driver jar, is done automatically. If you're running Rails 1.2.x, you'll need to modify environment.rb. Add the following snippet inside the Rails::Initializer block:

      if RUBY_PLATFORM =~ /java/
        require 'jdbc/mysql' #per jdbc-mysql-5.0.4/README.txt
      end

9. Create a testapp_development database, grants for testapp user, and a widgets table in MySQL (use migrations if you like).

10. Scaffold Widgets CRUD

      jruby script/generate scaffold widget

11. Start up the server

      jruby script/server

12. In your browser, go to http://localhost:3000/widgets.

And that's about it. You've now got a scaffolded widget page running in JRuby over JDBC to MySQL.

Troubleshooting

  • Scaffold fails with an error about "nonexistent jdbc adapter": Ensure that you've added the require line to environment.rb.
  • Scaffold and script/server terminate without running: Make sure you've successfully installed the activerecord-jdbc-adapter gem. The additional require in environment.rb causes Rails scripts to die silently if there are any errors.
  • Scaffold fails with the error "cannot convert NilClass into String": Make sure you've correctly specified the driver and url lines in database.yml
  • You see the following error when using SQL Server:
    &nbsp;&nbsp;&nbsp;com.microsoft.sqlserver.jdbc.AuthenticationJNI<br/> &nbsp;&nbsp;&nbsp;&nbsp;{clinit}<br/> &nbsp;&nbsp;&nbsp;WARNING: Failed to load the sqljdbc_auth.dll
    See I am having a weird JNI problem. Help me!
  • You have foxy_fixtures plugin installed and you get the following error:
    &nbsp;'load_missing_constant':<br/> &nbsp; uninitialized constant ActiveRecord::ConnectionAdapters::MysqlAdapter (NameError)
    This error occurs because foxy_features ships with built-in active_record adapters for MySQL, SQLite and PostgreSQL. Specifying any of the jdbc:{dbname}'s, or just jdbc, will cause foxy_fixtures to load the MySQL Ruby driver by default. This, obviously, will fail since there is no native MySQL driver for JRuby. To be able to run the app, the easy fix is to take the plugin out—uninstall it. Development and testing with foxy_fixtures and activerecord-jdbc still needs to be researched.
Clone this wiki locally