Griffon + GSQL mini howto - No Fluff Just Stuff

Griffon + GSQL mini howto

Posted by: Andres Almiray on June 9, 2009

Inspired by Geertjan's posts on Griffon and wizards (part 1, part 2) here is how you could embed a database into your Griffon application.


1. Create a datasource script similar to those found in Grails, place it at griffon-app/conf/DataSource.groovy
2. Create a database schema script, this will be used to generate tables and columns, place it at griffon-app/conf/DatabaseSchema.groovy. Mind the magic builder variable, it will be explained shortly
3. Create a script that contains data used to populate the database, place it at griffon-app/conf/Bootstrap.groovy. Mind the magic sql variable, it will also be explained in the following step
4. Create a DbService class under griffon-app/services/DbService.groovy with the following contents
A few things to notice here:

  • DbService is implemented as a singleton, this task is dead simple thanks to @Singleton
  • DbService.initDataSource() performs 3 tasks:
    1. initializes a database connection.
    2. generates the database schema.
    3. populates the database with bootstrap data.
Now you know where the builder and sql magic variables come from.


5. We must instruct DbService to load the database, the earliest opportunity is after the application has bootstrapped itself, we can react to an event (BootstrapEnd) by placing the following code in griffon-app/conf/Events.groovy
Here is how step #1 joins the rest, parsing configuration files with Groovy is very simple. This script is executed outside of the EDT always.


6. Let's create the view, a basic form with 2 textFields (name & lastname) that will be used to add a new person to the list of available persons, plus a table view displaying all persons
Now it would be a good time to drop in additional dependencies required by this little application, place the following on your app's lib directory

7. These are the contents of the app's model, notice the addition of a PropertyChangeListener to control the button's enabled state
8. Finally the last piece of the puzzle, the controller. Upon initialization this controller will query the database for all persons, iterate over the result set creating a new POGO (a Map actually) and insert it into the list of persons held by the model, GlazedLists takes care of the rest (keeping tables in sync with GlazedLists is that easy, really!). You will notice that while a new person is added to the in memory list it is not added to the database, the main reason being that the database is also an in memory one, changes are lost across sessions (this however can be tweaked easily too). Here are a few screenshots of the application in action



This is far from finished, for example there is no datasource pooling nor transaction support; the datasource definition is quite simple, it doesn't honor environments as Grails' datasources do; db calls are explicit, there are no domain classes. However this simplistic approach may work for small applications or even applets.

Full source code for this sample application is available at github, Griffon 0.1.1 is required to run this application.

Keep on Groovying!
Andres Almiray

About Andres Almiray

Andres is a Java/Groovy developer and a Java Champion with more than 20 years of experience in software design and development. He has been involved in web and desktop application development since the early days of Java. Andres is a true believer in open source and has participated on popular projects like Groovy, Griffon, and DbUnit, as well as starting his own projects (Json-lib, EZMorph, GraphicsBuilder, JideBuilder). Founding member of the Griffon framework and Hackergarten community event. https://ch.linkedin.com/in/aalmiray

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »