July 2, 2008

TOTD #37: SQLite3 with Ruby-on-Rails on GlassFish Gem

Categories: totd, web2.0


The default database for Rails 2.0.x application is SQLite3. This database is bundled with Mac OSX Leopard and so makes it really easy to get started with Ruby-on-Rails. But it requires couple of additional steps if you are using JRuby.

TOTD #28 explains how to create a simple CRUD application using Rails 2.0.x. It uses MySQL database which is strongly recommended for deployment. This TOTD (Tip Of The Day) provides complete steps to run a Ruby-on-Rails application using JRuby and GlassFish Gem with the default database, i.e. SQLite3.

  1. Create a Rails application as:

    ~/samples/jruby >~/testbed/jruby-1.1.2/bin/jruby -S rails runner
          create 
          create  app/controllers
          create  app/helpers
          create  app/models
          create  app/views/layouts
    . . .
          create  log/server.log
          create  log/production.log
          create  log/development.log
          create  log/test.log

    The generated “database.yml” looks like:

    # SQLite version 3.x
    #   gem install sqlite3-ruby (not necessary on OS X Leopard)
    development:
      adapter: sqlite3
      database: db/development.sqlite3
      timeout: 5000

    # Warning: The database defined as “test” will be erased and
    # re-generated from your development database when you run “rake”.
    # Do not set this db to the same as development or production.
    test:
      adapter: sqlite3
      database: db/test.sqlite3
      timeout: 5000

    production:
      adapter: sqlite3
      database: db/production.sqlite3
      timeout: 5000

  2. SQLite3 adapter is installed for the native Ruby bundled with Leopard. But in order to use SQLite3 with JRuby, you need to install SQLite3 JDBC adapter as shown below:
    ~/samples/jruby/runner >~/testbed/jruby-1.1.2/bin/jruby -S gem install activerecord-jdbcsqlite3-adapter
    JRuby limited openssl loaded. gem install jruby-openssl for full support.
    http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
    Successfully installed activerecord-jdbc-adapter-0.8.2
    Successfully installed jdbc-sqlite3-3.5.8
    Successfully installed activerecord-jdbcsqlite3-adapter-0.8.2
    3 gems installed
    Installing ri documentation for activerecord-jdbc-adapter-0.8.2…
    Installing ri documentation for jdbc-sqlite3-3.5.8…
    Installing ri documentation for activerecord-jdbcsqlite3-adapter-0.8.2…
    Installing RDoc documentation for activerecord-jdbc-adapter-0.8.2…
    Installing RDoc documentation for jdbc-sqlite3-3.5.8…
    Installing RDoc documentation for activerecord-jdbcsqlite3-adapter-0.8.2…
  3. Create a new file as “db/development.sqlite3″. It can be easily created using the “touch” command. See the beauty of SQLite that “db:create” is not required :)
  4. Update the development section of “database.yml” such that it looks like:
    development:
      adapter: jdbcsqlite3
      database: db/development.sqlite3
      timeout: 5000
  5. Create a simple scaffold as shown below:
    ~/samples/jruby/runner >~/testbed/jruby-1.1.2/bin/jruby script/generate scaffold run distance:float minutes:integer
    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/runs
          exists  app/views/layouts/
          exists  test/functional/
          exists  test/unit/
          exists  public/stylesheets/
          create  app/views/runs/index.html.erb
          create  app/views/runs/show.html.erb
          create  app/views/runs/new.html.erb
          create  app/views/runs/edit.html.erb
          create  app/views/layouts/runs.html.erb
          create  public/stylesheets/scaffold.css
          create  app/controllers/runs_controller.rb
          create  test/functional/runs_controller_test.rb
          create  app/helpers/runs_helper.rb
           route  map.resources :runs
      dependency  model
          exists    app/models/
          exists    test/unit/
          exists    test/fixtures/
          create    app/models/run.rb
          create    test/unit/run_test.rb
          create    test/fixtures/runs.yml
          create    db/migrate
          create    db/migrate/20080630211244_create_runs.rb

    and run the migration as

    ~/samples/jruby/runner >~/testbed/jruby-1.1.2/bin/jruby -S rake db:migrate
    (in /Users/arungupta/samples/jruby/runner)
    == 20080630205502 CreateRuns: migrating =======================================
    — create_table(:runs)
    &nbs
    p;  -> 0.0410s
       -> 0 rows
    == 20080630205502 CreateRuns: migrated (0.0420s) ==============================
  6. A Rails application is deployed on GlassFish from it’s parent directory. Therefore the application needs to be updated so taht exact location of database is specified. Basically you need to edit “database.yml” and the updated “development” fragment looks like:
    development:
      adapter: jdbcsqlite3
      database: runner/db/development.sqlite3
      timeout: 5000
  7. Run the application on GlassFish v3 gem as:
    ~/samples/jruby >~/testbed/jruby-1.1.2/bin/jruby -S glassfish_rails runner
    Jun 30, 2008 1:52:08 PM com.sun.enterprise.glassfish.bootstrap.ASMain main
    INFO: Launching GlassFish on HK2 platform
    Jun 30, 2008 1:52:08 PM com.sun.enterprise.glassfish.bootstrap.ASMainHK2 findDerbyClient
    INFO: Cannot find javadb client jar file, jdbc driver not available
    Jun 30, 2008 1:52:09 PM com.sun.enterprise.v3.services.impl.GrizzlyProxy start
    INFO: Listening on port 3000
    Jun 30, 2008 1:52:09 PM com.sun.enterprise.v3.services.impl.GrizzlyEmbeddedHttpConfigurator configureSSL
    WARNING: pewebcontainer.all_ssl_protocols_disabled
    Jun 30, 2008 1:52:09 PM com.sun.enterprise.v3.services.impl.GrizzlyEmbeddedHttpConfigurator configureSSL
    WARNING: pewebcontainer.all_ssl_ciphers_disabled
    Jun 30, 2008 1:52:09 PM com.sun.enterprise.v3.services.impl.GrizzlyProxy start
    INFO: Listening on port 3131
    Jun 30, 2008 1:52:09 PM com.sun.enterprise.v3.services.impl.GrizzlyProxy start
    INFO: Listening on port 3838
    Jun 30, 2008 1:52:09 PM com.sun.enterprise.v3.admin.adapter.AdminConsoleAdapter setContextRoot
    INFO: Admin Console Adapter: context root: /admin
    Jun 30, 2008 1:52:09 PM com.sun.grizzly.jruby.RailsAdapter startRubyRuntimePool
    INFO: Starting Rails instances
    Jun 30, 2008 1:52:16 PM  
    SEVERE: JRuby limited openssl loaded. gem install jruby-openssl for full support.
    http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
    Jun 30, 2008 1:52:17 PM com.sun.grizzly.jruby.RubyObjectPool$1 run
    INFO: JRuby and Rails instance instantiation took : 7998ms
    Jun 30, 2008 1:52:17 PM org.glassfish.scripting.rails.RailsDeployer load
    INFO: Loading application runner at /
    Jun 30, 2008 1:52:17 PM com.sun.enterprise.v3.server.AppServerStartup run
    INFO: Glassfish v3 started in 9430 ms

After adding couple of entries, “http://localhost:3000/runs” looks like:

So now you can use SQLite3 as your development database for Rails applications running on GlassFish v3 Gem.

Here are some useful pointers:

  • Running Rails with ActiveRecord-JDBC
  • SQLite3 man page provides complete set of commands for this database.
  • GlassFish v3 Gem 0.3.1
  • Rails powered by the GlassFish Application Server
  • Getting Started with JRuby on GlassFish

Please leave suggestions on other TOTD (Tip Of The Day) that you’d like to see. A complete archive is available here.

Technorati: totd rubyonrails jruby ruby sqlite sqlite3 glassfish v3 gem

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot
Related posts:
  1. TOTD #26: Overriding Database Defaults in Rails 2.0.2
  2. TOTD #104: Popular Ruby-on-Rails applications on GlassFish v3 – Redmine, Typo, Substruct
  3. TOTD #76: JRuby 1.2, Rails 2.3, GlassFish Gem 0.9.3, ActiveRecord JDBC Adapter 0.9.1 – can they work together ?
  4. TOTD #110: JRuby on Rails application using Oracle on GlassFish
  5. TOTD #111: Rails Scaffold for a pre-existing table using Oracle and GlassFish

5 Comments »

  1. Hi!

    I just wanted to tell you, that using:
    database: db/jdbc:sqlite:development.sqlite3
    in database.yml seem "overly" complex.

    Just let it be:
    database: db/development.sqlite3
    and it works.

    Using the 1st version, creates a file called "jdbc:sqlite:development.sqlite3" in the db directory, whereas the 2nd version uses the correct development.sqlite3 file.

    Comment by Rene A. — July 27, 2008 @ 12:14 pm

  2. Rene, thanks for the tip! I’ve updated the blog entry to reflect that.

    Comment by Arun Gupta — July 28, 2008 @ 10:25 am

  3. For some reason I cannot get past step 5. After setting the adapter to jdbcsqlite3 in the database.yml file and running the scaffold command I get the following error.
    I’m on Leopard 1.5.4 and I’m using JRuby 1.1.2 with Rails 1.2.6.

    JRuby limited openssl loaded. gem install jruby-openssl for full support.
    http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
    /Users/test/Downloads/jruby-1.1.2/lib/ruby/gems/1.8/gems/activerecord-1.15.6/lib/active_record/connection_adapters/abstract/connection_specification.rb:210:in `establish_connection’: database configuration specifies nonexistent jdbcsqlite3 adapter (ActiveRecord::AdapterNotFound)
    from /Users/test/Downloads/jruby-1.1.2/lib/ruby/gems/1.8/gems/activerecord-1.15.6/lib/active_record/connection_adapters/abstract/connection_specification.rb:202:in `establish_connection’
    from /Users/test/Downloads/jruby-1.1.2/lib/ruby/gems/1.8/gems/activerecord-1.15.6/lib/active_record/connection_adapters/abstract/connection_specification.rb:195:in `establish_connection’
    from /Users/test/Downloads/jruby-1.1.2/lib/ruby/gems/1.8/gems/rails-1.2.6/lib/initializer.rb:235:in `initialize_database’
    from /Users/test/Downloads/jruby-1.1.2/lib/ruby/gems/1.8/gems/rails-1.2.6/lib/initializer.rb:92:in `process’
    from /Users/test/Downloads/jruby-1.1.2/lib/ruby/gems/1.8/gems/rails-1.2.6/lib/initializer.rb:47:in `run’
    from /Users/test/Desktop/runner/./script/../config/../config/environment.rb:13:in `/Users/test/Desktop/runner/./script/../config/../config/environment.rb’
    from /Users/test/Desktop/runner/./script/../config/../config/environment.rb:27:in `require’
    from /Users/test/Downloads/jruby-1.1.2/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require’
    from /Users/test/Downloads/jruby-1.1.2/lib/ruby/gems/1.8/gems/rails-1.2.6/lib/commands/generate.rb:1:in `/Users/test/Downloads/jruby-1.1.2/lib/ruby/gems/1.8/gems/rails-1.2.6/lib/commands/generate.rb’
    from /Users/test/Downloads/jruby-1.1.2/lib/ruby/gems/1.8/gems/rails-1.2.6/lib/commands/generate.rb:27:in `require’
    from /Users/test/Downloads/jruby-1.1.2/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require’
    from script/generate:3

    Here’s a list of all the gems I have installed in the JRuby 1.1.2 distribution I’m using.

    *** LOCAL GEMS ***

    actionmailer (1.3.6)
    actionpack (1.13.6)
    actionwebservice (1.2.6)
    activerecord (1.15.6)
    activerecord-jdbc-adapter (0.8.2)
    activerecord-jdbcsqlite3-adapter (0.8.2)
    activesupport (1.4.4)
    jdbc-sqlite3 (3.5.8)
    rails (1.2.6)
    rake (0.8.1)
    rspec (1.1.4)
    sources (0.0.1)

    Any idea what is going on?

    Comment by comctrl6 — July 28, 2008 @ 5:00 pm

  4. Trying with Rails 2.1 and JRuby 1.2.2 I can get past the scaffold generation but when I try to run the migration I get the following error:

    ActiveRecord::ActiveRecordError: statement is not executing: SELECT version FROM schema_migrations

    I guess this stuff doesn’t actually work.

    I’m happy to hear what you have to say about it.

    Comment by comctrl6 — July 29, 2008 @ 6:18 pm

  5. comctrl6, What was the error for scaffold generation :) May be others can benefit from that.

    I’m using JRuby 1.1.3 and Rails 2.1.0. Other than that the list of gems is quite similar:

    actionmailer (2.1.0)
    actionpack (2.1.0)
    activerecord (2.1.0)
    activerecord-jdbc-adapter (0.8.2)
    activerecord-jdbcsqlite3-adapter (0.8.2)
    activeresource (2.1.0)
    activesupport (2.1.0)
    glassfish (0.3.1)
    jdbc-sqlite3 (3.5.8)
    rails (2.1.0)
    rake (0.8.1)
    rspec (1.1.4)
    sources (0.0.1)
    warbler (0.9.9)

    Try asking on alias.

    Comment by Arun Gupta — July 30, 2008 @ 9:20 am

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress
91043 visits from Sep 11, 2009