Skip to main content

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

Posted by arungupta on July 2, 2008 at 6:21 AM PDT


cellspacing="5">
style="width: 327px; height: 97px;" alt=""
src="http://www.sqlite.org/images/SQLite.gif">
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.





href="http://blogs.sun.com/arungupta/entry/totd_28_getting_started_with">TOTD
#28
explains how to create a simple CRUD application using Rails 2.0.x. It
uses MySQL database
which is href="http://www.mysql.com/products/enterprise/support.html">strongly
recommended for
deployment. This TOTD (Tip Of The Day) provides complete steps to run a
Ruby-on-Rails application using JRuby and href="http://rubyforge.org/projects/glassfishgem/">GlassFish
Gem with the default database, i.e. SQLite3.

  1. Create a Rails application as:


    cellpadding="2" cellspacing="2">
    style="font-family: monospace;">~/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:


    cellpadding="2" cellspacing="2">
    style="font-family: monospace;"># 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 style="font-family: monospace;">
  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:


    cellpadding="2" cellspacing="2">
    ~/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:


    cellpadding="2" cellspacing="2">
    development:

      adapter: jdbcsqlite3

      database: db/development.sqlite3

      timeout: 5000
  5. Create a simple scaffold as shown below:


    cellpadding="2" cellspacing="2">
    ~/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


    cellpadding="2" cellspacing="2">
    ~/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:


    cellpadding="2" cellspacing="2">
    development:

      adapter: jdbcsqlite3

      database: runner/db/development.sqlite3

      timeout: 5000
  7. Run the application on GlassFish v3 gem as:


    cellpadding="2" cellspacing="2">
    ~/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:



src="http://blogs.sun.com/arungupta/resource/ror/totd37-sqlite3.png">



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



Here are some useful pointers:

  • href="http://wiki.jruby.org/wiki/Running_Rails_with_ActiveRecord-JDBC">Running
    Rails with ActiveRecord-JDBC
  • href="http://developer.apple.com/documentation/Darwin/Reference/ManPages/man1/sqlite3.1.html">SQLite3
    man page provides complete set of commands for this database.
  • href="http://blogs.sun.com/arungupta/entry/rails_glassfish_gem_0_3">GlassFish
    v3 Gem 0.3.1
  • href="http://developers.sun.com/appserver/reference/techart/rails_gf/">Rails
    powered by the GlassFish Application Server
  • href="http://wiki.glassfish.java.net/Wiki.jsp?page=GettingStartedGuide">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 href="http://blogs.sun.com/arungupta/tags/totd">here.



Technorati: totd
rubyonrails
jruby href="http://technorati.com/tag/ruby">ruby href="http://technorati.com/tag/sqlite">sqlite href="http://technorati.com/tag/sqlite3">sqlite3 href="http://technorati.com/tag/glassfish">glassfish
v3 href="http://technorati.com/tag/gem">gem

Related Topics >>