Christoph Hartmann on April 16th, 2009

In most cases a Grails application uses a database to store its content. Fortunately Grails is tightly coupled with GORM to ease the use. Since Grails distinguish between different deployment environments, it uses a special configuration for development and production. Therefore you have to think of a database in production environment while you don not in development stage.

Glassfish v2 and v3 Prelude comes with an internal database JavaDB (based on the Apache Derby) that could be used for first tests. It would be great to use the default database by default for deployments for Glassfish via JNDI. This article shows how to use the bundled database with an Grails application.

In preparation for the next steps you have to install Grails 1.1 and Glassfish v2 or Glassfish v3 Prelude.

To create the demo app just run the following at command line:

grails create-app glassfishapp
cd glassfishapp
grails create-domain-class Bookmark
grails create-controller Bookmark

As the next step edit the domain class and the controller. E.g. open Eclipse with installed Groovy plugin.

class BookmarkController {
    def scaffold = Bookmark
}
class Bookmark {
    static constraints = {
    }
 
    String url
}

Now you are ready to test your app with


grails run-app

To prepare the Grails application for Glassfish the data source for the production environment has to be changed. Open the DataSource.groovy and change

production {
	dataSource {
		dbCreate = "update"
		url = "jdbc:hsqldb:file:prodDb;shutdown=true"
	}
}

to

production {
	dataSource {
		dbCreate = "update"
		// do not use java:/jdbc as documented
		jndiName = "jdbc/__default"
		username = "APP"
		password = "APP"
	}
}

Do not use jndiName = "java:jdbc/__default" as documented. Be aware that it may be dangerous to use dbCreate= "update" in production environments.

Now compile the web archive with

grails war

Start your database and the application server
./asadmin start-database
./asadmin start-domain

Finally open the sun web interface at http://localhost:4848/ to deploy the war file

http://localhost:8080/glassfishapp-0.1/

I experienced some memory problems by using Grails with Glassfish. To fix that problem I added -XX:MaxPermSize=256m and -Xmx1024m as JVM option. JVM options can be changed via the web interface (Common Tasks -> Application Server -> JVM Settings -> JVM Options)

Grails running in Glassfish

Grails running in Glassfish

Tags: , ,

Leave a Reply