Pivotal Knowledge Base

Follow

Using JNDI Data Sources with Grails Deployed on tcServer or Tomcat (2008392_draft)

Using JNDI Data Sources with Grails Deployed on tcServer or Tomcat (2008392_draft)

Purpose

This knowledge base article will explain how to use a JNDI data source in a Grails application.  There are two parts to successfully configuring a JNDI datasource.  The first is to configure your web container to define a JNDI data source and the second is to configure the Grails application to use the  JNDI data source from the container.

This knowledge base article will demonstrate how to configure a JNDI data source in a manner that is compatible for, Tomcat 6.0 and tcServer.  Configuration will vary for other containers like Jetty, WebLogic, Resin and WebSphere.

Resolution

Step 1    

The first step is to create and configure a JNDI DataSource.  With Tomcat 6.0, and tcServer there are two main ways to do this.  Both methods are outlined below, please choose the method that works best for your particular project.


Project Specific DataSource Configuration

    To configure the JNDI DataSource so that it is specific to the project and so that it is included in the Grails application use the following steps.

            Navigate to the "web-app/META-INF" directory
            Create a file called "context.xml"
            Open "context.xml" with your favorite text editor
            Configure a < Resource > tag to  for your specific JDBC driver


        The following is an example context.xml file which uses the H2 JDBC driver and the Tomcat BasicDataSourceFactory for pooling.

            <?xml version="1.0" encoding="utf-8"? >
            <Context >
               <Resource name="jdbc/myDataSource"
                          auth="Container"
                          type="javax.sql.DataSource"
                          username="sa"
                          password="sa"
                          driverClassName="org.h2.Driver"
                          url="jdbc:h2:driverDb"
                          factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
                          maxActive="100"
                          maxIdle="30"
                          maxWait="10000">
            </Context>


        Here is a quick list of the advantages to this approach.

            Everything is contained internal to the application so all of the configuration is contained in one central place
            The data source is at the application level so changes to the data source only require a redeploy of the application and not a restart of the entire server.
            Good for smaller deployments and TEST/QA scenarios

    

    Global DataSource Configuration

        To configure the JNDI DataSource so that it is global to the entire server and thus able to be shared across multiple applications deployed on the same server use the following steps.

            Open the "serverl.xml" file from your Tomcat or tcServer instance.
            Locate the < GlobalNamingResources > tag and add a < Resource > tag.
            Now, navigate to the "web-app/META-INF" directory of the Grails application
            Create a file called "context.xml" and open "context.xml" with your favorite text editor
            Configure a < ResourceLink > tag


        Here is an example < GlobalNamingResources > block.  It uses the H2 JDBC driver and the Tomcat BasicDataSourceFactory for pooling.

            <GlobalNamingResources >
                <Resource name="jdbc/myDataSource"
                          auth="Container"
                          type="javax.sql.DataSource"
                          username="sa"
                          password="sa"
                          driverClassName="org.h2.Driver"
                          url="jdbc:h2:driverDb"
                          factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
                          maxActive="100"
                          maxIdle="30"
                          maxWait="10000">
            </GlobalNamingResources>

        Here is an example context.xml file.

            <?xml version="1.0" encoding="utf-8"? >
            <Context >
               <ResourceLink name="jdbc/myDataSource"
                              global="jdbc/myDataSource"
                              type="javax.sql.DataSource">
            </Context>

                 Here is a quick list of the advantages to this approach.

            Decouples the data source configuration from the WAR file which makes distribution of the application simpler
            Allows the Tomcat / tcServer administrator to manage the configuration rather than the application developer
            Allows the data source configuration to be shared by multiple applications
            Great for large scale applications and production deployments


Step 2

The second step is to configure the Grails application to use the JNDI data source.

Since Grails version 1.0, Grails has had the ability to use JNDI by simply adding the jndiName property to the DataSource.groovy file.

    Here is an example which uses the jndiName property to set the production data source to the JNDI URI "java:comp/env/jdbc/myDataSource".

        environments {
            ...
            production {
                dataSource {
                    dbCreate = "update"
                    jndiName = "java:comp/env/jdbc/MyDataSource"
                }
            }
        }

Additional Information

Additional details for the configuration of context.xml

    Tomcat 6 & tc Server: http://tomcat.apache.org/tomcat-6.0-doc/config/context.html
    Tomcat 7 & tc Server: http://tomcat.apache.org/tomcat-7.0-doc/config/context.html


Additional details about configuring a Grails application to use a JNDI DataSource:

    http://www.grails.org/JNDI+Data+Sources
©VMware 2013

Comments

Powered by Zendesk