Tuesday Mar 10, 2009

TOTD # 73: JRuby and GlassFish Integration Test #4: JRuby 1.2.0 RC2 + Rails 2.2.x + GlassFish v2 + Warbler


TOTD #70, #71, #72 shows the first three integration tests that I typically run to ensure that the latest JRuby and GlassFish versions work nicely with each other.  #70 showed how to create a trivial Rails application and run it using GlassFish Gem#71 showed how the same application can be deployed on GlassFish v3 Prelude#72 showed how to deploy the same application on GlassFish v3.

The next test in the series is to ensure WAR-based deployment of a Rails application continue to work on GlassFish v2. It also shows that JNDI database connection pooling also work as expected. The latest publicly available build is GlassFish v2.1.

Lets begin integration test #4.
  1. Install Warbler gem ...

    ~/tools/jruby-1.2.0RC2/samples/rails/runner >../../../bin/jruby -S gem install warbler
    JRuby limited openssl loaded. gem install jruby-openssl for full support.
    http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
    Successfully installed warbler-0.9.12
    1 gem installed
    Installing ri documentation for warbler-0.9.12...
    Installing RDoc documentation for warbler-0.9.12...
  2. Edit "config/database.yml" and change the production database adapater from:

    production:
      adapter: mysql
      encoding: utf8
      database: runner_production
      pool: 5
      username: root
      password:
      socket: /tmp/mysql.sock

    to

    production:
      adapter: jdbcmysql
      encoding: utf8
      database: runner_production
      pool: 5
      username: duke
      password: glassfish
      socket: /tmp/mysql.sock
      jndi: jdbc/runner_production
      driver: com.mysql.jdbc.Driver

    The changes are highlighted in bold.

    Notice "jndi" key/value pair is specified along with "username" and "password". The JNDI reference is created for the GlassFish domain later. The reference is not resolved when this adapter is used with the JRuby CLI and so it falls back to username/password. However this JNDI reference is correctly resolved during runtime when the application is deployed as a WAR file in GlassFish.
  3. Create and migrate the production database as:

    ~/tools/jruby-1.2.0RC1/samples/rails/runner >../../../bin/jruby -S rake db:create RAILS_ENV=production
    (in /Users/arungupta/tools/jruby-1.2.0RC1/samples/rails/runner)
    ~/tools/jruby-1.2.0RC1/samples/rails/runner >../../../bin/jruby -S rake db:migrate RAILS_ENV=production
    (in /Users/arungupta/tools/jruby-1.2.0RC1/samples/rails/runner)
    ==  CreateRunners: migrating ==================================================
    -- create_table(:runners)
       -> 0.1150s
       -> 0 rows
    ==  CreateRunners: migrated (0.1170s) =========================================

    Note, how "RAILS_ENV=production" is specified at the command-line to ensure the production environment.
  4. Copy MySQL Connector/J jar in GLASSFISH_HOME/lib as:

    ~/tools/glassfish/v2.1/glassfish/ >cp ~/tools/mysql-connector-java-5.1.6/mysql-connector-java-5.1.6-bin.jar ./lib

    This is required for connection to the MySQL database.
  5. Fire up GlassFish v2.1 as:

    ~/tools/glassfish/v2.1/glassfish/bin >./asadmin start-domain
    Starting Domain domain1, please wait.
    Default Log location is /Users/arungupta/tools/glassfish/v2.1/glassfish/domains/domain1/logs/server.log.
    Redirecting output to /Users/arungupta/tools/glassfish/v2.1/glassfish/domains/domain1/logs/server.log
    Domain domain1 started.
    Domain [domain1] is running [Sun GlassFish Enterprise Server v2.1 (9.1.1) (build b60e-fcs)] with its configuration and logs at: [/Users/arungupta/tools/glassfish/v2.1/glassfish/domains].
    Admin Console is available at [http://localhost:4848].
    Use the same port [4848] for "asadmin" commands.
    User web applications are available at these URLs:
    [http://localhost:8080 https://localhost:8181 ].
    Following web-contexts are available:
    [/web1  /__wstx-services runner ].
    Standard JMX Clients (like JConsole) can connect to JMXServiceURL:
    [service:jmx:rmi:///jndi/rmi://Macintosh-187.local:8686/jmxrmi] for domain management purposes.
    Domain listens on at least following ports for connections:
    [8080 8181 4848 3700 3820 3920 8686 ].
    Domain supports application server clusters and other standalone instances.

    The logs are created in "domains/domain1/logs/server.log". Optionally, you can specify "--verbose" on the command-line to dump the log on the console itself.
  6. Create JDBC connection pool as:

    ~/tools/glassfish/v2.1/glassfish/bin >./asadmin create-jdbc-connection-pool --datasourceclassname com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource --restype javax.sql.DataSource --property "User=duke:Password=glassfish:URL=jdbc\\:mysql\\://localhost/runner_production" jdbc/runner_pool
    Command create-jdbc-connection-pool executed successfully.

  7. Create JDBC resource as:

    ~/tools/glassfish/v2.1/glassfish/bin >./asadmin create-jdbc-resource --connectionpoolid jdbc/runner_pool jdbc/runner_production
    Command create-jdbc-resource executed successfully.
  8. Create Warbler config file as:

    ~/tools/jruby-1.2.0RC2/samples/rails/runner >../../../bin/jruby -S warble config
    cp /Users/arungupta/tools/jruby-1.2.0RC2/lib/ruby/gems/1.8/gems/warbler-0.9.12/generators/warble/templates/warble.rb config/warble.rb
  9. Edit "config/warble.rb" to bundle the required gems by adding the following fragment:

     # Include all gems which are used by the web application
      require "#{RAILS_ROOT}/config/environment"
      BUILD_GEMS = %w(warbler rake rcov)
      for gem in Gem.loaded_specs.values
        next if BUILD_GEMS.include?(gem.name)
        config.gems[gem.name] = gem.version.version
      end

    as specified here. And then explicitly specify the runtime gem dependency by adding the following line:

    config.gems += ["activerecord-jdbc-adapter"]

    right after the previous code fragment. The "activerecord-jdbc-adapter" dependency needs to be explicitly included because this is required only at the runtime and so not resolved correctly by the previous code fragment.
  10. And create the WAR file as:

    ~/tools/jruby-1.2.0RC2/samples/rails/runner >../../../bin/jruby -S warble
    mkdir -p tmp/war/WEB-INF/gems/specifications
    cp /Users/arungupta/tools/jruby-1.2.0RC2/lib/ruby/gems/1.8/specifications/rails-2.2.2.gemspec tmp/war/WEB-INF/gems/specifications/rails-2.2.2.gemspec
    mkdir -p tmp/war/WEB-INF/gems/gems
    . . .
    cp public/javascripts/prototype.js tmp/war/javascripts/prototype.js
    cp public/stylesheets/scaffold.css tmp/war/stylesheets/scaffold.css
    mkdir -p tmp/war/WEB-INF
  11. Deploy the WAR file ...

    ~/tools/jruby-1.2.0RC2/samples/rails/runner >~/tools/glassfish/v2.1/glassfish/bin/asadmin deploy runner.war
    Command deploy executed successfully.

    After adding few entries the page at "http://localhost:8080/runner/runners" looks like:


So we are able to deploy a trivial Rails application as WAR file on GlassFish v2.1 and also leverage the JDBC connection pooling, that passes Test# 4.

Later blogs will show the remainder of tests. The current set of tests are available using the tags rubyonrails+glassfish+integrationtest.

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

Technorati: totd rubyonrails glassfish v2 jruby warbler connectionpooling jdbc jndi integrationtest

Monday Sep 08, 2008

TOTD #44: JDBC Connection Pooling for Rails on GlassFish v3


TOTD #9 explained how to configure JDBC connection pooling for Rails application deployed on GlassFish v2. There are several benefits of using using the JDBC connection pools:
  • No need to create a new database connection for each Rails instance.
  • No need to specify your password in database.yml or create a hack to hide it.
  • No garbage collection of connection after each use.
And because of the above mentioned (and other reasons) an improved application performance, scalability and efficiency.
The only way to deploy a Rails application on GlassFish v2 is to create a WAR file using Warbler. That's a great option and is already used in production mode. GlassFish v3 takes that to the next level by allowing to deploy a Rails application without any further pacakging. This TOTD (Tip Of The Day explains how to achieve database connection pooling using GlassFish v3.
  1. Lets create a simple scaffold

    ~/samples/jruby >~/tools/jruby-1.1.3/bin/jruby -S rails jndi_rails2 -d mysql
    ~/samples/jruby/jndi_rails2 >~/tools/jruby-1.1.3/bin/jruby script/generate scaffold runner miles:float minutes:integer
    ~/samples/jruby/jndi_rails2 >~/tools/jruby-1.1.3/bin/jruby -S rake db:create
    ~/samples/jruby/jndi_rails2 >~/tools/jruby-1.1.3/bin/jruby -S rake db:migrate
  2. Install MySQL ActiveRecord JDBC adapter

    ~/samples/jruby/jndi_rails2 >~/tools/jruby-1.1.3/bin/jruby -S gem install activerecord-jdbcmysql-adapter
  3. Copy MySQL Connector/J jar to JAVA_HOME/lib/ext

    ~/samples/jruby/jndi_rails2 >sudo cp ~/tools/mysql-connector-java-5.1.6/mysql-connector-java-5.1.6-bin.jar /System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/lib/ext
  4. Start GlassFish v3 server as ...

    ~/tools/glassfish/v3/b23/glassfishv3-prelude/glassfish >java -Xmx512m -DJRUBY_HOME=/Users/arungupta/tools/jruby-1.1.3 -jar modules/glassfish-10.0-SNAPSHOT.jar
  5. Setup JDBC connection pool
    1. Create JDBC connection pool

      ~/samples/jruby/jndi_rails2 >~/tools/glassfish/v3/b23/glassfishv3-prelude/glassfish/bin/asadmin create-jdbc-connection-pool --datasourceclassname com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource --restype javax.sql.DataSource --property "User=duke:Password=duke:URL=jdbc\\:mysql\\://localhost/jndi_rails2_production" jdbc/jndi_rails2_pool 
    2. Create JDBC resource

      ~/samples/jruby/jndi_rails2 >~/tools/glassfish/v3/b23/glassfishv3-prelude/glassfish/bin/asadmin create-jdbc-resource --connectionpoolid jdbc/jndi_rails2_pool jdbc/jndi_rails2 
    3. Ping JDBC pool

      ~/samples/jruby >~/tools/glassfish/v3/b23/glassfishv3-prelude/glassfish/bin/asadmin ping-connection-pool jdbc/jndi_rails2_pool 
  6. Change the development database in config/database.yml to:

    development:
      adapter: jdbc
      jndi: jdbc/jndi_rails2
      driver: com.mysql.jdbc.Driver 
  7. Run your app as

    ~/samples/jruby >~/tools/glassfish/v3/b22/glassfishv3-prelude/glassfish/bin/asadmin deploy jndi_rails2
And chug along with your scaffold at http://localhost:8080/jndi_rails2/runners.

Technorati: totd rubyonrails jruby ruby glassfish v3 connectionpooling jndi jdbc mysql

Tuesday Sep 11, 2007

TOTD #9: Using JDBC connection pool/JNDI name from GlassFish in Rails Application

Using the instructions followed in JRuby Hack Day and taking some help from Nick, I figured out how to use the JDBC connection pools configured in GlassFish using the JNDI names.

All the commands given below are relevant for GlassFish but the same concept will work where ever you deploy your WARed up JRuby on Rails application.

  1. Follow the bullet #1 and #2 from here to create a new application and database with the following exceptions:
    1. Use the name "jndi_rails" instead of "RailsApplication9" for the Project Name.
    2. Make sure to select "Access database using JDBC" in bullet # 2.2. This will bundle ActionRecord-JDBC plugin in your application.
  2. Create database using bullet # 3 from here. Use the following command instead to create the database instead:

    mysqladmin -u root create jndi_rails_production
  3. Create Model and Controller as described in bullet #4 here.
  4. Configure Model and Controller
    1. Configure Model
      1. In the NetBeans IDE, expand "Database Configurations", "migrate" and open "001_create_greetings.rb". Change the "self.up" helper method such that it looks like:

        def self.up
          create_table :greetings do |t|
            t.column :data, :string
          end
        end
      2. Expand "Configuration", open "database.yml", change the database name for development configuration from "jndi_rails_development" to "jndi_rails_production".
      3. Right-select the project, select 'Run Rake Target', 'db', 'migrate'. This generates the appropriate database tables and the following is shown in the output window:

        (in C:/Users/Arun Gupta/Documents/NetBeansProjects/jndi_rails)
        == CreateGreetings: migrating =================================================
        -- create_table(:greetings)
        -> 0.2650s
        == CreateGreetings: migrated (0.2650s) ========================================
      4. Add data to the database tables and give appropriate privileges using the following commands in a shell window:

        C:\\Program Files\\MySQL\\MySQL Server 5.0\\bin>mysql -u root
        Welcome to the MySQL monitor. Commands end with ; or \\g.
        Your MySQL connection id is 14
        Server version: 5.0.45-community-nt MySQL Community Edition (GPL)

        Type 'help;' or '\\h' for help. Type '\\c' to clear the buffer.

        mysql> use jndi_rails_production;
        Database changed
        mysql> insert into greetings values (1, "Hello from MySQL JNDI pool!");
        Query OK, 1 row affected (0.03 sec)

        mysql> grant all on jndi_rails_production.\* to arun@localhost identified by 'noway';
        Query OK, 0 rows affected (0.26 sec)

        mysql> flush privileges;
        Query OK, 0 rows affected (0.16 sec)

        mysql> quit;
        Bye
      5. Change the production database environment from:

        production:
          adapter: mysql
          database: jndi_rails_production
          username: root
          password:
          host: localhost


        to

        production:
          adapter: jdbc
          jndi: jdbc/jndi_rails
          driver: com.mysql.jdbc.Driver

        Notice only JDBC adpater and JNDI name is specified in the database configuration. This ensures that the database is resolved using only the JNDI name. Although "database", "username" and "password" attributes may be specified in addition to "jndi" and "driver" attributes. In this case, the Rails configuration falls back to pure-Ruby MySQL adapter.
    2. Configure Controller using bullet # 5.2 from here.
  5. Create the JDBC connection pool and resource
    1. In GLASSFISH_HOME\\bin, create the JDBC connection pool by giving the following command:

      asadmin create-jdbc-connection-pool --datasourceclassname com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource --restype javax.sql.DataSource --property User=arun:Password=noway:URL=jdbc\\:mysql\\://localhost/jndi_rails_production jdbc/jndi_rails_pool

      The following output should be seen on the console:

      Command create-jdbc-connection-pool executed successfully.
    2. Create the JDBC resource by giving the following command:

      asadmin create-jdbc-resource --connectionpoolid jdbc/jndi_rails_pool jdbc/jndi_rails

      The following output should be seen on the console:

      Command create-jdbc-resource executed successfully.
  6. Create a WAR file as described in bullet #6 here
  7. Download, Install and Start GlassFish as described in bullet #7 here.
  8. Download and Install MySQL/J Connector in a new directory. Copy the JAR file from the main installation directory to 'GLASSFISH_HOME/lib' directory.
  9. Copy the WAR file (jndi_rails.war) in "domains/domain/autodeploy" directory.

The application is accessible at "http://localhost:8080/jndi_rails/say/hello".

An alternative approach to use the connection pools is discussed here. Lou also nicely describes the benefits of connection pooling.

Connecting to Oracle From Rails explains how to connect to Oracle (instead of JavaDB) with JRuby.

Please leave suggestions on other TOTD that you'd like to see. A complete archive is available here.

Technorati: totd rubyonrails jruby ruby netbeans glassfish connectionpooling jndi jdbc jrubyonglassfish mysql

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