Home > How-To: Using MySQL JNDI in Grails 1.3.3 + Tomcat 6

How-To: Using MySQL JNDI in Grails 1.3.3 + Tomcat 6
Posted by Kaleb Brasee on Thursday, August 19th, 2010 at 10:48 PM
Since Grails 1.3.3 has a known defect where database connections get closed when using the default software connection pool, I decided to set up my Grails app (http://www.wysiap.com) to use MySQL through a JNDI data source configured in Tomcat. I thought it would be simple, but it turned out to be a challenge to get it working for both the development server (running from the workspace) and the production server.

But eventually I did! Here's how I got both Grails + Tomcat + a JNDI data source working in both the development and production environments...

To set up the JNDI on the development server:
  1. Add the JNDI configuration to grails.naming.entries to grails-app/conf/Config.groovy in the application
    Here's what my configuration looked like (subtitute your own JNDI name, URL, username, password and any other config items you might want):
    grails.naming.entries = [
        "jdbc/myDataSource": [
            type: "javax.sql.DataSource",
            auth: "Container",
            description: "My data source",
            driverClassName: "com.mysql.jdbc.Driver",
            url: "jdbc:mysql://localhost:3306/myschema",
            username: "myuser",
            password: "mypassword",
            maxActive: "100",
            maxIdle: "30",
            maxWait: "10000"
        ]
    ]
    
  2. Add the MySQL driver JAR (downloadable at mysql.com) to the lib directory in the application

  3. Configure the application to use JNDI for a development datasource in grails-app/conf/DataSource.groovy
    Here's the applicable subset of my DataSource.groovy file:
    dataSource {
    	pooled = false
    }
    
    // other config stuff
    
    environments {
    	development {
    		dataSource {
    			dbCreate = "update"
    			jndiName = "java:comp/env/jdbc/myDataSource"
    		}
    	}
    
    	// other environments
    }
    

To set up the JNDI on the production server:
  1. Add the JNDI as a Resource in the Tomcat context file (context.xml, or a virtual server-specific context XML)
    Here's what my configuration looked like (subtitute your own JNDI name, URL, username, password and any other config items you might want):
    <Resource name="jdbc/myDataSource" auth="Container" type="javax.sql.DataSource"
        maxActive="100" maxIdle="30" maxWait="10000"
        username="myuser" password="mypassword" driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://localhost:3306/myschema" />
    
  2. Add the MySQL driver JAR (downloadable at mysql.com) to the lib directory in the Tomcat server

  3. Configure the application to use JNDI for a production datasource in grails-app/conf/DataSource.groovy
    Here's the applicable subset of my DataSource.groovy file:
    dataSource {
    	pooled = false
    }
    
    // other config stuff
    
    environments {
    	// other environments
    
    	production {
    		dataSource {
    			dbCreate = "update"
    			jndiName = "java:comp/env/jdbc/myDataSource"
    		}
    	}
    }