Skip to main content

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

Posted by arungupta on September 12, 2007 at 6:50 AM PDT

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.

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

Technorati:
totd
rubyonrails
jruby
netbeans
glassfish
connectionpooling
jndi
jdbc
jrubyonglassfish

Related Topics >>