Wednesday, August 22, 2012

Grails, Scaffolding, GSPs

I've spent the last few evenings reading up on Grails Domain Classes and Controllers, the 'M' and 'C' portions of MVC, respectively.

I have been working through Getting Started with Grails, Second Edition by Scott Davis and Jason Rudolph

Throughout the process of working through their Racetrack sample application, I have been switching between MongoDB 2.0.6 and MySQL 5.5.25. 

In the process, I have hit some bumps in the road regarding MongoDB Issues with Grails Scaffolding that do not occur in MySQL

I posted these on StackOverflow and mentioned in LinkedIn discussions.  This has led me down the path of going so far as to trying to download a tag from GitHub for newer builds of the MongoDB Plugin for Grails (as of today 2012-08-22, 1.0.0.GA is the latest official release for this plug-in).

I've also learned a lot about MySQL in the process of setting it up, securing it on Fedora 17. 

Neat things I've learned:

Now, I'm starting to learn about the 'V' aspect of MVC in Grails.  I am digging into GSPs. 

Wednesday, August 15, 2012

MySQL on Fedora 17 and Grails

I was running into some strange behavior when writing a test Grails Application that uses MongoDB (MongoDB 2.0.6-2.fc17 installed via YUM) for storage.

Specifically, when I use scaffolding, I am unable to reorder the fields using the static constraints block.

I saw this mentioned on a post in the Nabble forum for Grails, alluding to this possibly being a problem with MongoDB.  However, I was not able to find any more information on scaffold field ordering via static constraints not working for MongoDB.

In order to run this down further, I decided to take my same test application and run it using MySQL for storage.  I haven't used MySQL for years, so it took a little to get re-acclimated.

  1. Quick check for MySQL on the Fedora 17 server:
    # yum list mysql
    Loaded plugins: langpacks, presto, refresh-packagekit
    Installed Packages
    mysql.x86_64                       5.5.25a-1.fc17                       @updates
    Available Packages
    mysql.i686                         5.5.25a-1.fc17                       updates
    

    Yay, it's already installed. Looks like it shipped with 5.5.25a-1, a specific build for FC17.

  2. Enable the Service upon startup:
    # systemctl enable mysqld.service
    

  3. Secure it:
    # mysqladmin -u root password ********
    

  4. Enable remote connections from development machine for administration.
    # mysql -u root -p mysql
    mysql> GRANT ALL PRIVILEGES ON *.* TO root@'DEV_MACHINE_HOSTNAME' IDENTIFIED BY ********;
    
    Also, I opened up port 3306 through the firewall on the FC17 server.

  5. Next, I logged in via MySQL Client and ran Create Database, Create User,  and Grant All to create a new Database for my Grails Application, create a User for this Database, and Grant all privileges to that User for the newly-created database.
  6. Finally, I changed my Grails App Plugins (removed mongodb, installed mysql-connectorj-5.1.12 and hibernate-2.1.0) and updated DataSource.groovy:
    dataSource {
        pooled = true
        driverClassName = "com.mysql.jdbc.Driver"
        dialect = "org.hibernate.dialect.MySQL5InnoDBDialect"
        username = "racetrack"
        password = "********"
    }
    hibernate {
        cache.use_second_level_cache = true
        cache.use_query_cache = true
        cache.provider_class = "net.sf.ehcache.hibernate.EhCacheProvider"
    }
    
    // environment specific settings
    environments {
        development {
          dataSource {
             dbCreate = "create-drop" // one of 'create', 'create-drop','update'
             url = "jdbc:mysql://ptenn:3306/racetrack"
          }
       }
       test {
          dataSource {
             dbCreate = "create"
             url = "jdbc:mysql://ptenn:3306/racetrack"
          }
       }
       production {
          dataSource {
             dbCreate = "update"
             url = "jdbc:mysql://ptenn:3306/racetrack"
          }
       }
    }
    

Monday, August 13, 2012

Fedora transition: SysVinit to Systemd

I installed Fedora 17 on a Linux Server (previously had been working with Fedora 14), and ran across a surprise.

Apparently, Fedora is transitioning from SysVinit to Systemd: Fedora features/systemd

I realized something was amiss when attempting to set runlevels for 2 services: sshd and mongod.

I ran the following commands:

# chkconfig sshd on
# chkconfig mongod on

and proceeded to reboot the server.

My understanding is that these commands should turn runlevels 2, 3, 4, 5 on for both sshd and mongod services.

When I restarted, I was able to verify via testing that these 2 services came up as expected.

However, when I went to run:
# chkconfig --list

These services did not appear in the list.

After doing some more research, I found the following guide: Fedora SysVinit to Systemd Cheatsheet.

Ran the new equivalent of chkconfig --list:

# ls /etc/systemd/system/*.wants

...

/etc/systemd/system/multi-user.target.wants:
abrt-ccpp.service     chronyd.service         nfs-lock.service
abrtd.service         crond.service           remote-fs.target
abrt-oops.service     cups.path               rpcbind.service
abrt-vmcore.service   irqbalance.service      rsyslog.service
acpid.service         mcelog.service          sendmail.service
arp-ethers.service    mdmonitor.service       smartd.service
atd.service           mongod.service          sm-client.service
auditd.service        multipathd.service      sshd.service
avahi-daemon.service  NetworkManager.service

...
and saw my sshd.service and mongod.service as expected.
It will take some time to get used to this new syntax for systemd ... so used to chkconfig and service.

Wednesday, August 8, 2012

Grails and MongoDB: Setup

I am attempting to create a sample Grails project that uses MongoDB for persistent store. 

I've learned a lot about Grails Plugins while trying to build a simple Grails App with MongoDB.

First stop was this blog post on MongoDB titled "Grails in the Land of MongoDB".

My blog post is based on this posting from MongoDB, with additional notes and information that I discovered while following these steps. 
The blog post "Grails in the Land of MongoDB" listed steps as follows:

Step 1: Remove the hibernate dependency from the BuildConfig.groovy file.

This wasn't as straightforward as I thought.  I am using Grails 2.1.0.  After I ran the command:

grails create-app doctor

I opened up the BuildConfig.groovy file.  It had the following plugins:

    
plugins {
        runtime ":hibernate:$grailsVersion"
        runtime ":jquery:1.7.2"
        runtime ":resources:1.1.6"

        // Uncomment these (or add new ones) to enable additional resources capabilities
        //runtime ":zipped-resources:1.0"
        //runtime ":cached-resources:1.0"
        //runtime ":yui-minify-resources:0.1.4"

        build ":tomcat:$grailsVersion"

        runtime ":database-migration:1.1"

        compile ':cache:1.0.0'
    }

I removed the entries for Hibernate and Database Migration

After making these changes, my plugins section of BuildConfig.groovy appeared as follows:

    
    plugins {
        runtime ":jquery:1.7.2"
        runtime ":resources:1.1.6"

        // Uncomment these (or add new ones) to enable additional resources capabilities
        //runtime ":zipped-resources:1.0"
        //runtime ":cached-resources:1.0"
        //runtime ":yui-minify-resources:0.1.4"

        build ":tomcat:$grailsVersion"

        compile ':cache:1.0.0'
    }

Step 2:Install the MongoDB GORM
I ran the command:

grails install-plugin mongodb

This did the following (as far as I can tell): 



  1. Added an entry to my application.properties: plugins.mongodb=1.0.0.GA
  2. Created the directory projects/doctor/plugins/mongodb-1.0.0.GA in my .grails directory.
Step 3:Edit DataSource.groovy to specify connection settings
There was a slight typo in the MongoDB blog. The port should actually be 27017.

grails { 
  mongo { 
    host = "localhost"
    port = 27017 
    username = "doctorUser"
    password="medicine"
    databaseName = "doctors"
  } 
}