GlassFish v3 is the Reference Implementation for Java EE 6. Following the "extensibility" principle of Java EE 6, it also allows Ruby-on-Rails, Groovy and Grails and Python/Django applications to be seamlessly deployed as well, without any additional packaging. This blog has published multiple entries on deploying a Rails application on GlassFish as given below:

  • TOTD #105: Monitor Rails application using JavaScript
  • TOTD #104: Redmine, Typo, Substruct on GlassFish v3
  • TOTD #84: Apache + mod_proxy_balancer to load balance Rails applications on GlassFish
  • TOTD #81: nginx to load balance Rails applications on GlassFish Gem
  • TOTD #73: Deploying Rails application as WAR on GlassFish v2.1
  • TOTD #72: Deploying Rails application on GlassFish v3
  • TOTD #70: Deploying Rails application on GlassFish Gem
All the existing applications have used JavaDB, SQLite3, or MySQL as the database so far. In the process of getting ready for the upcoming Oracle Open World 2009, this Tip Of The Day will show how to use an Oracle database with a JRuby-on-Rails application deployed on GlassFish v3.

Lets get started!

  1. Install Oracle database as explained in TOTD #106.
  2. Configure JRuby/Rails in GlassFish v3 using one of the mechanisms explained in TOTD #104. Alternatively you can also install the GlassFish gem as:
    >./bin/jruby -S gem install glassfish JRuby limited openssl loaded. gem install jruby-openssl for full support. http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL Successfully installed rack-1.0.0 Successfully installed glassfish-0.9.5-universal-java 2 gems installed Installing ri documentation for rack-1.0.0... Installing ri documentation for glassfish-0.9.5-universal-java... Installing RDoc documentation for rack-1.0.0... Installing RDoc documentation for glassfish-0.9.5-universal-java... This blog will use GlassFish Gem for running the application described below.
  3. Create a new database user and grant rights using SQL*Plus as shown:

    Macintosh-187:~ oracle$ sqlplus "/ as sysdba" SQL*Plus: Release 10.2.0.4.0 - Production on Thu Oct 1 12:32:33 2009 Copyright (c) 1982, 2007, Oracle. All Rights Reserved. Connected to: Oracle Database 10g Release 10.2.0.4.0 - Production SQL> CREATE USER glassfish IDENTIFIED BY glassfish DEFAULT tablespace users TEMPORARY tablespace temp; User created. SQL> GRANT CONNECT TO glassfish IDENTIFIED BY glassfish; Grant succeeded. SQL> GRANT UNLIMITED TABLESPACE TO glassfish; Grant succeeded. SQL> GRANT CREATE TABLE TO glassfish; Grant succeeded. SQL> GRANT CREATE SEQUENCE TO glassfish; Grant succeeded. SQL> exit Disconnected from Oracle Database 10g Release 10.2.0.4.0 - Production The user name and password are chosen as "glassfish" for simplicity. This is not a recommended setting for production usage though.
  4. Create a simple Rails application

    1. Create a simple Rails application as:

      jruby -S rails bookstore -d oracle
    2. Using the normal "jdbc" adapter will give the following error later:

      ActionView::TemplateError (book_url failed to generate from {:controller=>"books", :action=>"show", :id=>#<Book id: #<BigDecimal:3feef1eb,'10000.0',1(8)>, title: "Ultramarathon Man", author: "Dean Karnazes", created_at: "2009-10-06 00:03:14", updated_at: "2009-10-06 00:03:14">}, expected: {:controller=>"books", :action=>"show"}, diff: {:id=>#<Book id: #<BigDecimal:459bdb65,'10000.0',1(8)>, title: "Ultramarathon Man", author: "Dean Karnazes", created_at: "2009-10-06 00:03:14", updated_at: "2009-10-06 00:03:14">}) on line #13 of app/views/books/index.html.erb: As evident, the "id" column is returned as BigDecimal where as it should be integer. Fortunately the fix is simple, install the "oracle_enhanced_adapter" (docs) as:

      bookstore >~/tools/jruby/bin/jruby -S gem install activerecord-oracle_enhanced-adapter JRuby limited openssl loaded. gem install jruby-openssl for full support. http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL Successfully installed activerecord-oracle_enhanced-adapter-1.2.2 1 gem installed Installing ri documentation for activerecord-oracle_enhanced-adapter-1.2.2... Installing RDoc documentation for activerecord-oracle_enhanced-adapter-1.2.2... Using this "enhanced adapter" is highly recommended for connecting with Oracle databases from Rails applications.
    3. Edit "config/database.yml" and change the "development" section to:

      development: adapter: oracle_enhanced driver: oracle.jdbc.driver.OracleDriver url: jdbc:oracle:thin:@localhost:1521:orcl username: glassfish password: glassfish Notice, the username and password values are the same as chosen in the SQL statements above.
    4. Generate a scaffold as:

      bookstore >~/tools/jruby/bin/jruby script/generate scaffold book title:string author:string JRuby limited openssl loaded. gem install jruby-openssl for full support. http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL exists app/models/ exists app/controllers/ exists app/helpers/ create app/views/books exists app/views/layouts/ exists test/functional/ exists test/unit/ create test/unit/helpers/ exists public/stylesheets/ create app/views/books/index.html.erb create app/views/books/show.html.erb create app/views/books/new.html.erb create app/views/books/edit.html.erb create app/views/layouts/books.html.erb create public/stylesheets/scaffold.css create app/controllers/books_controller.rb create test/functional/books_controller_test.rb create app/helpers/books_helper.rb create test/unit/helpers/books_helper_test.rb route map.resources :books dependency model exists app/models/ exists test/unit/ exists test/fixtures/ create app/models/book.rb create test/unit/book_test.rb create test/fixtures/books.yml create db/migrate create db/migrate/20091005233152_create_books.rb
    5. Prepare your application for JDBC as:

      bookstore >~/tools/jruby/bin/jruby script/generate jdbc JRuby limited openssl loaded. gem install jruby-openssl for full support. http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL exists config/initializers create config/initializers/jdbc.rb exists lib/tasks create lib/tasks/jdbc.rake
    6. Migrate the database as:
      ~/samples/v3/rails/oracle/bookstore >~/tools/jruby/bin/jruby -S rake db:migrate (in /Users/arungupta/samples/v3/rails/oracle/bookstore) == CreateBooks: migrating ================================================== == -- create_table(:books) -> 0.0740s -> 0 rows == CreateBooks: migrated (0.0750s) ===========================================
  5. Lets run the application as:
    ~/samples/v3/rails/oracle/bookstore >~/tools/jruby/bin/jruby -S glassfish -l Starting GlassFish server at: 129.145.133.197:3000 in development environment... Writing log messages to: /Users/arungupta/samples/v3/rails/oracle/bookstore/log/development.log. Press Ctrl+C to stop. Oct 6, 2009 9:45:51 AM com.sun.enterprise.v3.services.impl.GrizzlyProxy start INFO: Listening on port 3000 . . . he application is now accessible at "http://localhost:3000/books" and looks like:



    Click on "New Book" and enter the values as shown:



    Click on "Create" to see the output as:



    Click on "Back" to see the main page as:



    After adding another book, this page looks like:



    And another book …



So we created a brand new JRuby/Rails application and ran it using GlassFish and Oracle backend. A subsequent blog entry will show how to create a similar application using an existing database.

A complete archive of all the TOTDs is available here. The complete list of Rails blog entries are available here.

This and other similar applications will be demonstrated at the upcoming Oracle Open World.

Technorati: totd oracle database glassfish v3 jruby rails oow

Related posts:
  1. TOTD #28: Getting Started with Rails 2.0 Scaffold
  2. TOTD #70: JRuby and GlassFish Integration Test# 1: JRuby 1.2.0 RC1 + Rails 2.2.x + GlassFish Gem
  3. TOTD #14: How to generate JRuby-on-Rails Controller on Windows (#9893)







More...