There's something to be said for things that just work right out of the box, and JEE integration with Grails falls nicely into that category.  When people first hear about Grails and they see demonstrations of its rapid application development (RAD) capabilities, it’s not long before an air of skepticism seems to set in.  Surely a framework offering this level of productivity can’t possibly play nicely with a corporation’s highly-configurable and often complex application servers.  Of course, this isn’t a new topic, as we've certainly explored some of these areas before.  Nevertheless, questions remain, and any new technology has to prove its worth.  

One rather common question concerns the ability to hook into an app server’s data sources using JNDI.  If you've invested in a JEE container, there's a good chance you want to make use of its database connection pooling.  (For that matter, it's probably a company policy to do so.)  And, needless to say, beyond just connection pooling, using data sources also offers the added security and maintenance benefits of not packaging your database user ID, password, and URL inside your application.

So, what does it take to get a Grails app to obtain its database connections from a container-managed data source?  Assuming you have your data source configured in your app server, it’s as simple as telling Grails how to look it up.  Let’s first take a look at the data source definition in the app server.  For this example, we’ll look at a MySQL data source configured in JBoss Application Server.

$ cat /Applications/jboss-4.0.5.GA/server/default/deploy/mysql-ds.xml
<?xml version="1.0" encoding="UTF-8"?>

Once we know the JNDI name for the data source – in this case, we see from the output above that it’s jdbc/racetrack – we simply define a Spring bean for that data source in $PROJECT_HOME/spring/resources.xml (where $PROJECT_HOME is the root directory of your Grails application).

$ cat racetrack/spring/resources.xml
<?xml version="1.0" encoding="UTF-8"?>
    <bean id="dataSource"
        <property name="jndiName" value="

Then, just run grails war to create the WAR, and that’s it.  Now, when you deploy the WAR to your app server, your Grails application will use your container-managed data source for all of its database access.

If you found this particular example to be a bit trivial, great!  That’s exactly the point.  JEE integration doesn’t have to be painful, at least not with Grails.  


  • JBoss configuration file for MySQL data source - mysql-ds.xml
  • Spring configuration file - resources.xml
  • The rest of this mysterious racetrack sample application - Coming soon!