Miles to go …

August 19, 2007

TOTD #3: Using JavaDB with JRuby on Rails

Filed under: web2.0 — arungupta @ 11:00 pm

In a previous screencast, I showed how to develop a Rails application fetching data from the MySQL database and deploy it in GlassFish. GlassFish comes pre-bundled with JavaDB. Based upon a user request, this TOTD shows to use JavaDB database instead of MySQL.

Here are the steps required to replace MySQL database configuration with JavaDB. You can either mix these steps with the existing screencast or follow these steps altogether after the original app is created using MySQL.

  1. Copy "GLASSFISH_HOME/javadb/lib/derbyclient.jar" to "C:\Program Files\NetBeans 6.0M10\ruby1\jruby-1.0\lib" (or the directory location where JRuby for NetBeans 6 is installed). Make sure NetBeans IDE is restarted after the jar file is copied.
  2. In your application, change development database configuration in "database.yml" from:

    development:
      adapter: mysql
      database: glassfishrocks_development
      username: root
      password:
      host: localhost

    to

    development:
      adapter: jdbc
      driver: org.apache.derby.jdbc.ClientDriver
      url: jdbc:derby://localhost:1527/sample
      username: app
      password: app

    The NetBeans IDE comes pre-configured with the database URL, username and password used in this configuration.

  3. Start JavaDB using "asadmin start-database" command.
  4. Invoke db:migrate rake target to create the database required by the application. The development database configuration is used to create the database tables. If you followed the screencast, then a new table "greetings" is created in the database "sample".

  5. Change production database configuration in "database.yml" from:

    production:
      adapter: mysql
      database: glassfishrocks_production
      username: root
      password:
      host: localhost

    to

    production:
      adapter: jdbc
      driver: org.apache.derby.jdbc.ClientDriver
      url: jdbc:derby://localhost:1527/sample
      username: app
      password: app

    This new database configuration is exactly the same as for the development configuration. The production configuration is used by the WAR file.

  6. Insert value into the newly create database as "insert into GREETINGS values (1, 'Hello from JavaDB');".

  7. Create the WAR file using war:standalone:create rake target.
  8. Deploy the WAR file in the "GLASSFISH_HOME/domains/domain/autodeploy" directory.

You can also check out an early version of a tutorial that is being written about installing and configuring Ruby support in the NetBeans IDE. If you would like to get early access to Ruby documentation, learn more here.

Please leave suggestions on the TOTD that you’d like to see. A complete archive is available here.

Technorati: totd jrubyonglassfish jruby rubyonrails javadb glassfish ror ruby

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • email
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot
Related posts:
  1. Database-enabled RoR WAR on GlassFish
  2. ActiveRecord-JDBC 0.5 – simplified database configuration
  3. TOTD #9: Using JDBC connection pool/JNDI name from GlassFish in Rails Application
  4. TOTD #35: Rails Database Connection on Solaris
  5. TOTD # 73: JRuby and GlassFish Integration Test #4: JRuby 1.2.0 RC2 + Rails 2.2.x + GlassFish v2 + Warbler

2 Comments »

  1. Excellent, thanks!

    Comment by Glen — August 20, 2007 @ 8:20 am

  2. Just to elaborate a bit on Step 6, to add the sample row in JavaDB, you can either use NetBeans (Services Tab, add the DerbyClient.jar, then right-click it and choose "Connect Using…" to create the database listing) or the JavaDB "ij" command-line tool located under $GLASSFISH_HOME/javadb/frameworks/NetworkServer.

    Comment by Glen — August 26, 2007 @ 7:28 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment

The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.
Powered by WordPress