Thursday, September 18, 2014

Tomcat 7 JNDI DataSource with Groovy Grails

Tomcat 7 JNDI DataSource with Groovy Grails


This article describes how to configure JNDI DataSource to integrate Tomcat 7 with application built in Groovy on Grails framework.

Environment setup

I suppose that you have properly set following environment variables:
export ORACLE_HOME=/u01/app/oracle/product/11.2.0/xe
export CATALINA_HOME=/opt/apache-tomcat-7.0.55

Open shell for Tomcat user

sudo su tomcat7
cp $ORACLE_HOME/jdbc/lib/ojdbc6.jar $CATALINA_HOME/lib
# in my case:
# cp /u01/app/oracle/product/11.2.0/xe/jdbc/lib/ojdbc6.jar /opt/apache-tomcat-7.0.55/lib

Edit server.xml

Open file $CATALINA_HOME/conf/server.xml

Find appropriate Host section
Add following data source definition:

<Context docBase="YourApplication-0.1" path="/YourApplication-0.1" reloadable="true">
 <Resource name="jdbc/yourApplication" auth="Container"
  type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
  username="DatabaseUser" password="DatabasePassword" maxActive="20" maxIdle="10"

Restart tomcat7

sudo /etc/init.d/tomcat7 restart

Open your DataSource.groovy

Add or modify dataSource section:

environments {
 demo {
  dataSource {
   dialect = "org.hibernate.dialect.Oracle10gDialect"
   dbCreate = "none"
   pooled = false
   jndiName = "java:comp/env/jdbc/yourApplication"


Do not forget prefix java:comp/env/ in your jndiName in Grails, otherwise it will not match your Tomcat settings and may result to following (not much explaining) error:
Failed to retrieve JNDI naming context for container [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/YourApplication-0.1]]  so no cleanup was performed for that container


You can change schema by setting:
hibernate.default_schema = 'schemaName'
(Note that I have not yet tested this config parameter)

Rebuild your application

cd YourApplication
grails -Dgrails.env=demo war

Deploy your application

cd target
cp -f YourApplication-0.1.war $CATALINA_HOME/webapps


No comments: