Thursday, September 18, 2014

Tomcat 7 JNDI DataSource with Groovy Grails


Tomcat 7 JNDI DataSource with Groovy Grails


Intro

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"
  url="jdbc:oracle:thin:@127.0.0.1:1521:xe"
  username="DatabaseUser" password="DatabasePassword" maxActive="20" maxIdle="10"
  maxWait="-1"/>
</Context>

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"
  }
 }
}

Note

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

Note2

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

Done

Tuesday, September 09, 2014

Tomcat 7 Linux CentOS installation


Tomcat 7 Linux CentOS installation


# get app
Get Tomcat7
wget http://www.eu.apache.org/dist/tomcat/tomcat-7/v7.0.55/bin/apache-tomcat-7.0.55.tar.gz

# check md5
md5sum apache-tomcat-7.0.55.tar.gz
xmd5sum=3c46fc0f608c1280dcd65100a983f285
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
#!/bin/bash
#
# Tomcat 7
#
# chkconfig: 234 80 20
# description:  Start up the Tomcat servlet engine.
# Source function library.
. /etc/init.d/functions
RETVAL=$?
CATALINA_HOME="/opt/tomcat7"
# export JAVA_HOME=/usr/java/latest
case "$1" in
 start)
  if [ -f $CATALINA_HOME/bin/startup.sh ];
   then
    echo $"Starting Tomcat"
    /bin/su -s /bin/bash tomcat7 $CATALINA_HOME/bin/startup.sh
  fi
  ;;
 stop)
  if [ -f $CATALINA_HOME/bin/shutdown.sh ];
   then
    echo $"Stopping Tomcat"
     /bin/su -s /bin/bash tomcat7 $CATALINA_HOME/bin/shutdown.sh
  fi
  ;;
 *)
  echo $"Usage: $0 {start|stop}"
  exit 1
  ;;
esac
exit $RETVAL
EOF
# 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
echo Installation DONE
echo
echo To start Tomcat7 service, type:
echo service tomcat7 start
echo
echo To stop Tomcat7 service, type:
echo service tomcat7 stop
echo

# make tomcat users
# source http://tecadmin.net/steps-to-install-tomcat-server-on-centos-rhel/
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" />
EOF


Tuesday, August 26, 2014

Install Oracle Sun Java to CentOS


Install Oracle Sun Java to CentOS

Download Oracle Java 7 JDK for Linux:
http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html
I prefer RPM. The tar.gz is an alternative available mainly for non-root users.

Note for x86 and x64
You can choose between x86 and x64 versions. You cannot install both together from RPM package. You can install one from RPM and the second manually from tar.gz.

Download (accept licence):
http://download.oracle.com/otn-pub/java/jdk/7u67-b01/jdk-7u67-linux-x64.rpm

Download (shell command)
wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/7u67-b01/jdk-7u67-linux-x64.rpm

Install java from rpm:
rpm -i *.rpm

Upgrade Java (optional):
rpm -Uvh jdk-7u-linux-x64.rpm

Setup Java
## java
alternatives --install /usr/bin/java java /usr/java/latest/jre/bin/java 200000

## javaws
alternatives --install /usr/bin/javaws javaws /usr/java/latest/jre/bin/javaws 200000

## Install javac only if you installed JDK (Java Development Kit) package
alternatives --install /usr/bin/javac javac /usr/java/latest/bin/javac 200000
alternatives --install /usr/bin/jar jar /usr/java/latest/bin/jar 200000

Choose Oracle Java as default
alternatives --config java
alternatives --config javaws
alternatives --config javac

Setup path
Add to /etc/profile file or ~/.bash_profile or to ~/.bashrc
export JAVA_HOME="/usr/java/latest"
Java JDK and JRE absolute version (/usr/java/jdk1.7.0_67)

Check version
java -version
javac -version

Resources

Oracle Java install doc:
http://docs.oracle.com/javase/7/docs/webnotes/install/
http://docs.oracle.com/javase/7/docs/webnotes/install/linux/linux-jdk.html

All steps in detail at:
http://www.if-not-true-then-false.com/2010/install-sun-oracle-java-jdk-jre-7-on-fedora-centos-red-hat-rhel/

Auto accept licence:
http://stackoverflow.com/questions/10268583/how-to-automate-download-and-installation-of-java-jdk-on-linux

Done


Install Jenkins in CentOS Linux


Install Jenkins in CentOS Linux

Jenkins
Jenkins is successor of Hudson project. It serves as continuous integration platform. Jenkins integrates with CVS, SVN, Git, Mercurial hg. Supports MVN, cron-like scheduling, shell commands, remote deployment and more.

Install Oracle Java
JBoss requires original Oracle Sun Java or compatible VM. As first step, Install Oracle Java.

Install Jenkins
wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo
rpm --import http://pkg.jenkins-ci.org/redhat/jenkins-ci.org.key
yum install jenkins

Change port
/etc/sysconfig/jenkins
JENKINS_AJP_PORT="7009"
JENKINS_PORT="7070"

Start Jenkins
service jenkins start

Configure authentication
http://www.linuxfunda.com/2013/11/17/how-to-install-and-configure-jenkins-on-centos-6-4/

Done

Resources
src: https://wiki.jenkins-ci.org/display/JENKINS/Installing+Jenkins+on+RedHat+distributions

Further step:

Install Mercurial
yum search mercurial
yum install mercurial.x86_64

Install JBoss in CentOS Linux


How to install JBoss in CentOS Linux


Install Oracle Java
JBoss requires original Oracle Sun Java or compatible VM. As first step, Install Oracle Java.

Download JBoss 7.1.1 Final
http://jbossas.jboss.org/downloads/
wget http://download.jboss.org/jbossas/7.1/jboss-as-7.1.1.Final/jboss-as-7.1.1.Final.zip

Install JBoss
I followed this step by step how to:
http://www.davidghedini.com/pg/entry/install_jboss_7_on_centos


Changes against above doc

Java settings moved from bash_profile to bashrc to let it work after sudo su

We have original Java from Oracle, therefore I edited /etc/init.d/jboss and commented Java setup:
# Load Java configuration.
# [ -r /etc/java/java.conf ] && . /etc/java/java.conf
# export JAVA_HOME

In /etc/init.d/jboss change setting for chkconfig (no hyphen):
chkconfig: 234 80 20


Additional setup commands:
mkdir -p /var/run/jboss-as/ /etc/jboss-as/ /var/log/jboss-as/
chown jboss.jboss /var/run/jboss-as/ /etc/jboss-as/ /var/log/jboss-as/
cp /usr/share/jboss-as/bin/init.d/jboss-as.conf /etc/jboss-as/jboss-as.conf

Note for Jenkins and Oracle
To prevent interference with Jenkins and Oracle XE ports I did following changes:
In Jenkins I changed AJP port to 7009.
In Jenkins I changed web port to 7070.
In Oracle XE I changed web admin port to 6060.

Change in /etc/sysconfig/jenkins
JENKINS_AJP_PORT="7009"
JENKINS_PORT="7070"

Add user in JBoss
cd /usr/share/jboss-as/bin
./add-user.sh
./add-user.sh
login: jboss
pass: jboss_admin_password
Added user 'jboss' to file '/usr/share/jboss-as/standalone/configuration/mgmt-users.properties'
Added user 'jboss' to file '/usr/share/jboss-as/domain/configuration/mgmt-users.properties'

Done
You can access JBoss in local address: http://localhost:7070/
Or by using port forwarding http://localhost:7000/ and http://localhost:7009/



Solving errors


Error:
jboss Context initialization failed: java.lang.OutOfMemoryError: PermGen space

Solution:
Open file /usr/share/jboss-as/bin/standalone.conf
Insert following line at the beginning of this file:

export JAVA_OPTS="-server -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -Xms256m -Xmx1g -XX:PermSize=64M -XX:MaxPermSize=1g"

This will change settings for garbage collector, heap size and perm size. You can adjust memory settings as you need. After configuration, save file, restart jboss:

/etc/init.d/jboss restart



Data Source configuration


Open /usr/share/jboss-as/standalone/configuration/standalone.xml

Find datasources and add datasource section for Oracle:

            <datasources>
                <datasource jndi-name="java:jboss/yourDataSourceName" pool-name="yourDataSourceName" enabled="true">
                    <connection-url>jdbc:oracle:thin:@127.0.0.1:1521:xe</connection-url>
                    <driver>com.oracle</driver>
                    <pool>
                        <min-pool-size>3</min-pool-size>
                        <max-pool-size>5</max-pool-size>
                    </pool>
                    <security>
                        <user-name>oracle_user</user-name>
                        <password>oracle_password</password>
                    </security>
                    <validation>
                        <exception-sorter class-name="org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter"/>
                    </validation>
                    <timeout>
                        <blocking-timeout-millis>5000</blocking-timeout-millis>
                        <idle-timeout-minutes>5</idle-timeout-minutes>
                    </timeout>
                </datasource>
                <drivers>
                    <driver name="com.oracle" module="com.oracle">
                        <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
                    </driver>
                </drivers>
            </datasources>


Add Oracle JARs to JBoss

mkdir -p /usr/share/jboss-as/modules/com/oracle/main
cp /u01/app/oracle/product/11.2.0/xe/jdbc/lib/*.jar /usr/share/jboss-as/modules/com/oracle/main
chown -R jboss.jboss /usr/share/jboss-as/modules/com/oracle

Create modules configuration

Create file /usr/share/jboss-as/modules/com/oracle/main/module.xml as follows:

<module xmlns="urn:jboss:module:1.0" name="com.oracle">
    <resources>
        <resource-root path="ojdbc6.jar"/>
    </resources>
    <dependencies>
        <module name="javax.api"/>
    </dependencies>
</module>


Restart JBoss:

/etc/init.d/jboss restart

You can also check your data sources in JBoss admin console.

Done.


Oracle XE database configuration failed


Oracle XE 11g (Express edition) installation error in CentOS Linux

Error:

/etc/init.d/oracle-xe configure
Starting Oracle Net Listener...Done
Configuring database...
Database Configuration failed.  Look into /u01/app/oracle/product/11.2.0/xe/config/log for details

Solution:

Your host was not specified in /etc/hosts. You can solve it by command executed under root user:
sudo su
echo "127.0.0.1   "`hostname` >> /etc/hosts



Solve Dependencies:

Install required packages by entering commands:
sudo su
yum install libaio bc flex
rpm -i oracle-xe-11.2.0-1.0.x86_64.rpm

Install Oracle XE express edition


Oracle XE 11g (Express edition) installation steps in CentOS Linux

This article will show you how to install and configure Oracle XE 11g in CentOS Linux step by step.

Setup hosts file
echo "127.0.0.1   "`hostname` >> /etc/hosts

Install required packages
yum install libaio bc flex

Download Oracle XE 11g R2 installation package:
You can choose from enterprise, standard or express:
http://www.oracle.com/technetwork/database/enterprise-edition/downloads/index-092322.html
I chose express for our project.

Download page:
http://www.oracle.com/technetwork/database/database-technologies/express-edition/downloads/index.html

Direct link (accept licence):
http://download.oracle.com/otn/linux/oracle11g/xe/oracle-xe-11.2.0-1.0.x86_64.rpm.zip

Install Oracle XE
unzip oracle-xe-11.2.0-1.0.x86_64.rpm.zip
cd Disk1
rpm -i oracle-xe-11.2.0-1.0.x86_64.rpm


Auto configuration (optional):
ORACLE_LISTENER_PORT=1521
ORACLE_HTTP_PORT=8080 # or 6060
ORACLE_PASSWORD=enter_admin_password_here
ORACLE_CONFIRM_PASSWORD=enter_admin_password_here
ORACLE_DBENABLE=y
export ORACLE_LISTENER_PORT ORACLE_HTTP_PORT ORACLE_PASSWORD \
ORACLE_CONFIRM_PASSWORD ORACLE_DBENABLE

Start configuration:
/etc/init.d/oracle-xe configure

Configuration options:
If you have not set up auto install, you can change following settings:
http port 8080 (changed to 6060)
database port 1521
credentials: SYS and SYSTEM
pass: enter_admin_password_here
start database when system starts: yes


Add oracle to path
Add following command to global /etc/profile for all users or to .bashrc or .bash_profile for current user:
source /u01/app/oracle/product/11.2.0/xe/bin/oracle_env.sh

Change password for unix user oracle:
passwd oracle
enter_admin_password_here

Login as oracle:
su oracle

Start sqlplus
su oracle
sqlplus / as sysdba

Connect using role sysdba (optional)
# you can start and connect in two following steps:
su oracle
sqlplus /nolog
connect / as sysdba

Enter SQL commands
-- just FYI:
SELECT * FROM V$LOGFILE;
show parameter DB_RECOVERY_FILE_DEST;
show parameters sessions;
show parameters processes;

Setup Oracle params:
-- Enable remote access:
EXEC DBMS_XDB.SETLISTENERLOCALACCESS(FALSE);
-- max sessions:
alter system set sessions=250 scope=spfile;
shutdown immediate
startup
show parameters sessions;
-- max processes:
alter system set processes=200 scope=spfile;
shutdown immediate
startup
show parameters processes;
shutdown immediate
startup mount
-- archivelog:
alter database archivelog;
alter database open;
SELECT LOG_MODE FROM SYS.V$DATABASE;
-- Oracle replies
--  LOG_MODE
--  ------------
--  ARCHIVELOG
-- exit SQLplus:
EXIT

Setup password for APEX admin console (optional):
sqlplus / as sysdba
@/u01/app/oracle/product/11.2.0/xe/apex/apxchpwd.sql
-- enter password for admin user
-- exit SQLplus:
EXIT

Open APEX web admin:
http://localhost:8080/apex/f?p=4550:1
Note: I am currently using port 6060:
http://localhost:6060/apex/f?p=4550:1
Note2: Due to port forwarding, I am accessing:
http://localhost:7006/apex/f?p=4550:1
Workspace: Internal
User Name: admin
Password: your_password_from_previous_step
Note3: Admin interface link:
http://localhost:7006/apex/apex_admin

Environment variables check:
# check XE listener:
find /u01/app/oracle/product/11.2.0/xe/ | grep listener.ora
cat /u01/app/oracle/product/11.2.0/xe/network/admin/listener.ora
# env check:
set | grep -i ^oracle

Create Oracle user (prerequisities)
You have to create user in Linux and in Oracle DB to enable successfull authentication.
Oracle user names contain by default specific prefix. You can change this setting or you need to add this prefix to each new user created in Oracle.

Check user prefix in Oracle (optional, recommended)
su oracle
sqlplus / as sysdba
SHOW PARAMETER os_authent_prefix
-- by default, user prefix is set to ops$

Remove username prefix
(optional, recommended)
You can remove prefix and work without "ops$":

su oracle
sqlplus / as sysdba
create pfile='/tmp/pfile.txt' from spfile;
shutdown immediate;
exit
echo "os_authent_prefix=''" >> /tmp/pfile.txt

sqlplus / as sysdba
create spfile from pfile='/tmp/pfile.txt';
startup
show parameter os_authent_prefix

Create user in Linux
sudo su
useradd sqluser1
passwd sqluser1
su oracle
sqlplus / as sysdba

Create user in Oracle
(Linux authentication)
-- User identified by Linux password
CREATE USER sqluser1 IDENTIFIED EXTERNALLY;

Create user in Oracle
(with prefix, not recommended)
-- User with name prefix - not recommended
CREATE USER ops$sqluser1 IDENTIFIED EXTERNALLY;

Create user in Oracle
(Oracle authentication)
-- User identified by Oracle password
CREATE USER sqluser1 IDENTIFIED BY sqluser1_password;
GRANT CREATE SESSION, CREATE TABLE TO sqluser1;
-- alter user sqluser1 quota 100m on system ;
GRANT UNLIMITED TABLESPACE TO sqluser1 ;

Create user in Oracle
(all in one example)
su oracle
sqlplus / as sysdba
-- User identified by Oracle password
CREATE USER sqluser1 IDENTIFIED BY sqluser1_password;
-- ALTER USER sqluser1 QUOTA 100m ON system ;
GRANT UNLIMITED TABLESPACE TO sqluser1 ;
GRANT CREATE SESSION, ALTER SESSION, CREATE DATABASE LINK, -
  CREATE MATERIALIZED VIEW, CREATE PROCEDURE, CREATE PUBLIC SYNONYM, -
  CREATE ROLE, CREATE SEQUENCE, CREATE SYNONYM, CREATE TABLE, -
  CREATE TRIGGER, CREATE TYPE, CREATE VIEW, UNLIMITED TABLESPACE -
  TO sqluser1 ;

Grant rights to user
GRANT CONNECT TO username;
GRANT resource TO username;
GRANT EXECUTE on schema.procedure TO username;
GRANT SELECT [, INSERT] [, UPDATE] [, DELETE] on schema.table TO username;

grant CREATE SESSION, ALTER SESSION, CREATE DATABASE LINK, -
  CREATE MATERIALIZED VIEW, CREATE PROCEDURE, CREATE PUBLIC SYNONYM, -
  CREATE ROLE, CREATE SEQUENCE, CREATE SYNONYM, CREATE TABLE, -
  CREATE TRIGGER, CREATE TYPE, CREATE VIEW, UNLIMITED TABLESPACE -
  to sqluser1 ;

Grant role sysdba to user
GRANT SYSDBA to sqluser1;

Error
ORA-01950: no privileges on tablespace 'SYSTEM'
Solution
alter user sqluser1 quota 100m on system ;
alter user sqluser1 quota unlimited on system ;
or
GRANT UNLIMITED TABLESPACE TO sqluser1 ;

Resources

Installation steps inspired by:
http://www.davidghedini.com/pg/entry/install_oracle_11g_xe_on

User name prefix settings:
src: http://www.oracle-base.com/articles/misc/os-authentication.php

Useful commands

-- Show users
SELECT username, account_status FROM dba_users ;

-- Show current user name
show user

-- Change default schema for user
ALTER USER sqluser1 WITH DEFAULT_SCHEMA = sqluser1_schema1 ;

-- Drop all user data in schema
http://stackoverflow.com/questions/1690404/how-to-drop-all-user-tables

-- Show tables for users
SELECT owner, table_name FROM all_tables ;

-- Describe table
DESC table_name ;

-- Drop all tables
select 'drop table '||table_name||' cascade constraints;' from user_tables;

-- Drop sequences
set head off feed off verify off
define owner=sql_user_name
SELECT 'DROP TABLE &owner..' || TABLE_NAME || ' CASCADE CONSTRAINTS;' FROM DBA_TABLES WHERE OWNER=UPPER('&owner');

Useful DB tools

Oracle SQL Developer Download
SQLplus
Squirrel SQL Download


Monday, July 14, 2014

Eclipse Ggts and Grails memory settings

I started developing Groovy on Grails application in Eclipse Ggts. I experienced performance issues of Eclipse editor and application written in Groovy on Grails. Once I received following error:
java.lang.OutOfMemoryError: PermGen space

Following settings helped me to increase speed of Ggts and Grails app.

I adjusted settings of Ggts Eclipse ini file, i.e. I open:
C:\sw\ggts-3.5.1.RELEASE\GGTS.ini

and modified following argument to:
--launcher.XXMaxPermSize
512M

and modified following arguments:
-Xms512m
-Xmx2g
-XX:MaxPermSize=1g

and added following lines under list of arguments "-vmargs":
-XX:+UseConcMarkSweepGC
-XX:+CMSClassUnloadingEnabled
-XX:+CMSPermGenSweepingEnabled

Then I adjusted Debug configuration (created before). I open dialog: 
Debug Configurations / Grails / MyProfile / Arguments

and put following settings to VM Arguments:
-server -Xms512m -Xmx2g -XX:PermSize=32m -XX:MaxPermSize=1g -Dfile.encoding=UTF-8 -XX:+CMSClassUnloadingEnabled -XX:+UseConcMarkSweepGC

(src: http://groovy.codehaus.org/Running section Avoiding PermGen out of memory)

This simply increases memory consumption limit, so the Grails application will run smoother.

Other effective settings for VM Arguments:
-server -Xms128M -Xmx2G -XX:PermSize=128m -XX:MaxPermSize=1800m -Dfile.encoding=UTF-8 -XX:SurvivorRatio=128 -XX:MaxTenuringThreshold=0 -XX:+UseTLAB -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -XX:+CMSIncrementalMode -XX:-UseGCOverheadLimit -XX:+ExplicitGCInvokesConcurrent

These settings can be applied to grails-app/conf/BuildConfig.groovy as well.

(src: http://grails.org/plugin/tomcat section Using jvmArgs property)

Additional Tomcat notes ...

Grails - Apache Tomcat - JNDI DataSource settings, i.e. JDBC:
grails-app/conf/Config.groovy
More at http://grails.org/plugin/tomcat

Tomcat Admin user account settings:
C:\tomcat\conf\tomcat-users.xml