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


Tuesday, September 09, 2014

Tomcat 7 Linux CentOS installation

Tomcat 7 Linux CentOS installation

# get app
Get Tomcat7

# check md5
md5sum apache-tomcat-7.0.55.tar.gz
echo -n Checking md5 ...' '
if [ "`md5sum apache-tomcat-7.0.55.tar.gz | awk '{print $1}'`" != "$xmd5sum" ] ; then echo INCORRECT md5! Exiting. ; exit 1 ; else echo md5 OK ; fi

# add user
echo Adding users
groupadd tomcat7
useradd -s /bin/bash -g tomcat7 tomcat7

# unpack
echo Unpacking Tomcat
tar -zxf apache-tomcat-7.0.55.tar.gz
mv apache-tomcat-7.0.55 /opt/
ln -s apache-tomcat-7.0.55 /opt/tomcat7
chown -Rf tomcat7.tomcat7 /opt/apache-tomcat-7.0.55

# create init script
echo Creating init script

cat > /etc/init.d/tomcat7 <<EOF
# Tomcat 7
# chkconfig: 234 80 20
# description:  Start up the Tomcat servlet engine.
# Source function library.
. /etc/init.d/functions
# export JAVA_HOME=/usr/java/latest
case "$1" in
  if [ -f $CATALINA_HOME/bin/ ];
    echo $"Starting Tomcat"
    /bin/su -s /bin/bash tomcat7 $CATALINA_HOME/bin/
  if [ -f $CATALINA_HOME/bin/ ];
    echo $"Stopping Tomcat"
     /bin/su -s /bin/bash tomcat7 $CATALINA_HOME/bin/
  echo $"Usage: $0 {start|stop}"
  exit 1
exit $RETVAL
# end of init script

# register init script
chmod a+x /etc/init.d/tomcat7
chmod 755 /etc/init.d/tomcat7

# register init script
echo Registering Tomcat7 to run on system startup
chkconfig --level 234 jboss off
chkconfig --add tomcat7
chkconfig --level 234 tomcat7 on

# Final message
echo Installation DONE
echo To start Tomcat7 service, type:
echo service tomcat7 start
echo To stop Tomcat7 service, type:
echo service tomcat7 stop

# make tomcat users
# source
cat > /opt/tomcat7/conf/tomcat-users-example.xml <<EOF
<!-- user manager can access only manager section -->
<role rolename="manager-gui" />
<user username="manager" password="manager_password" roles="manager-gui" />
<!-- user admin can access manager and admin section both -->
<role rolename="admin-gui" />
<user username="admin" password="admin_password" roles="manager-gui,admin-gui" />