Tuesday Jul 01, 2008

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


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)
       -> 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:

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
About

profile image
Arun Gupta is a technology enthusiast, a passionate runner, author, and a community guy who works for Oracle Corp.


Java EE 7 Samples

Stay Connected

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today