Skip to main content

TOTD #31: CRUD Application using Grails - Hosted on GlassFish and MySQL

Posted by arungupta on April 23, 2008 at 7:00 AM PDT



href="http://blogs.sun.com/arungupta/entry/totd_30_crud_application_using">TOTD
#30 explained how to create CRUD application using href="http://grails.org/">Grails and hosted using
in-built Jetty servlet
engine
and in-memory HSQLDB
database. Jetty and HSQLDB are built into Grails and allows to start easily.
You can also use href="http://glassfish.dev.java.net">GlassFish and
MySQL for deploying your applications in
production environment.



This blog entry walks you through the steps of deploying a Grails
application on GlassFish and MySQL.

  1. If MySQL
    is already installed, then download href="https://glassfish.dev.java.net/downloads/v2ur1-b09d.html">GlassFish
    v2 UR1. Otherwise you can also Download href="http://glassfish.java.net/">GlassFish v2 UR1
    and MySQL co-bundle
    from usual Download
    Page
    ( href="http://docs.sun.com/app/docs/doc/820-3797/ggkei?l=en&q=mysql&a=view">instructions).
  2. Configure MySQL database
    1. Download MySQL Connector/J 5.1.6 from href="http://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.6.tar.gz/from/pick?file=Downloads/Connector-J/mysql-connector-java-5.1.6.tar.gz&mirror=pick#mirrors">here.
    2. Extract the bundle and copy
      "mysql-connector-java-5.1.6-bin.jar" to the "lib" directory of Grails
      application.
    3. Start MySQL database as:


      cellpadding="2" cellspacing="2">
      ~/testbed/grails-1.0.2/samples/crud
      >sudo
      /usr/local/mysql/bin/mysqld_safe --user root --console


      Starting mysqld daemon with databases from /usr/local/mysql/data
    4. Create database by giving the following command:


      cellpadding="2" cellspacing="2">
      ~/testbed/grails-1.0.2/samples/crud
      >/usr/local/mysql/bin/mysqladmin
      create crudProd --user root
  3. Configure the Application
    1. Edit
      "grails-app/conf/DataSource.groovy" to specify MySQL
      configuration. The updated file looks like (changes highlighted in style="font-weight: bold;">bold):


      cellpadding="2" cellspacing="2">
      dataSource
      {

             
      pooled = false

             
      driverClassName = "com.mysql.jdbc.Driver"

             
      username = "root"

             
      password = ""

              style="font-weight: bold;">dialect =
      "org.hibernate.dialect.MySQL5InnoDBDialect"

      }

      hibernate {

          cache.use_second_level_cache=true

          cache.use_query_cache=true

         
      cache.provider_class='org.hibernate.cache.EhCacheProvider'

      }

      // environment specific settings

      environments {

             
      development {

                     
      dataSource {

                             
      dbCreate = "create-drop" // one of 'create', 'create-drop','update'

                             
      url = "jdbc:hsqldb:mem:devDB"

                     
      }

              }

             
      test {

                     
      dataSource {

                             
      dbCreate = "update"

                             
      url = "jdbc:hsqldb:mem:testDb"

                     
      }

              }

             
      production {

                     
      dataSource {

                             
      dbCreate = "update"

                             
      url = "jdbc:mysql://localhost/crudProd"

                     
      }

              }

      }


    2. Being in production, it's recommended to use href="http://dev.mysql.com/doc/refman/5.0/en/innodb.html">InnoDB
      tables instead of
      href="http://dev.mysql.com/doc/refman/5.0/en/myisam-storage-engine.html">MyISAM
      tables. This can be easily specified by using the dialect as
      explained href="http://grails.markmail.org/message/efk2iv6a2fenhvnp?q=list:user">here.
      More details about the contents of "DataSource.groovy" can be found
      here.

    3. Create a WAR by giving the following command:


      cellpadding="2" cellspacing="2">
      ~/testbed/grails-1.0.2/samples/crud
      >grails war style="font-weight: bold;">


      Welcome to Grails 1.0.2 - http://grails.org/

      Licensed under Apache Standard License 2.0

      Grails home is set to: /Users/arungupta/testbed/grails-1.0.2



      Base Directory: /Users/arungupta/testbed/grails-1.0.2/samples/crud

      Note: No plugin scripts found

      Running script /Users/arungupta/testbed/grails-1.0.2/scripts/War.groovy

      Environment set to production

         [delete] Deleting:
      /Users/arungupta/.grails/1.0.2/projects/crud/resources/web.xml

         [delete] Deleting directory
      /Users/arungupta/.grails/1.0.2/projects/crud/classes

      . . .

      [propertyfile]
      Updating property file:
      /Users/arungupta/testbed/grails-1.0.2/samples/crud/staging/WEB-INF/classes/application.properties

          [mkdir] Created dir:
      /Users/arungupta/testbed/grails-1.0.2/samples/crud/staging/WEB-INF/plugins

           [copy] Warning:
      /Users/arungupta/testbed/grails-1.0.2/samples/crud/plugins not found.

            [jar] Building jar:
      /Users/arungupta/testbed/grails-1.0.2/samples/crud/crud-0.1.war

         [delete] Deleting directory
      /Users/arungupta/testbed/grails-1.0.2/samples/crud/staging

      Done creating WAR
      /Users/arungupta/testbed/grails-1.0.2/samples/crud/crud-0.1.war
  4. Deploy the WAR file as:


    cellpadding="2" cellspacing="2">
    ~/testbed/grails-1.0.2/samples/crud
    >~/testbed/glassfish/v3/p2b9/glassfish/bin/asadmin
    deploy crud-0.1.war


    crud-0.1 deployed successfully

    Command deploy executed successfully.



    The application is now accessible at "http://localhost:8080/crud-0.1"
    and looks like:



    src="http://blogs.sun.com/arungupta/resource/images/grails-crud-v2-default-page.png">

  5. READ - Click on "StateController" and the following page is
    shown:



    src="http://blogs.sun.com/arungupta/resource/images/grails-crud-v2-list-page.png">
  6. CREATE - Click on "New State" and enter the values as shown
    below:



    src="http://blogs.sun.com/arungupta/resource/images/grails-crud-v2-new-state-page.png">



    and click on "Create" to see the following page:



    src="http://blogs.sun.com/arungupta/resource/images/grails-crud-v2-created-page.png">
  7. UPDATE & DELETE - You can click on "Edit" or
    "Delete" to perform U or D of CRUD or click
    on "State List" to view the updated list.

Please leave suggestions on other TOTD that you'd like to see.
A complete archive is available href="http://blogs.sun.com/arungupta/tags/totd">here.



Technorati: groovy
grails
glassfish
mysql href="http://technorati.com/tag/scripting">scripting
netbeans

Related Topics >>

Comments

Thanks Carol, fixed the typo!

Arun, nice example. I got it running, but I think I found a typo in your instructions, you say create crudDev then you say the url is crudProd. These 2 should be the same right? mysqladmin create crudDev url = "jdbc:mysql://localhost/crudProd"