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

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.

Posted by Rene A. on July 27, 2008 at 05:14 AM PDT #

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

Posted by Arun Gupta on July 28, 2008 at 03:25 AM PDT #

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?

Posted by comctrl6 on July 28, 2008 at 10:00 AM PDT #

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.

Posted by comctrl6 on July 29, 2008 at 11:18 AM PDT #

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 user@jruby.codehaus.org alias.

Posted by Arun Gupta on July 30, 2008 at 02:20 AM PDT #

Post a Comment:
Comments are closed for this entry.
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