Sunday, June 23, 2013

Tomcat Configurations

Defining Users for Tomcat Server

We can add users for tomcat server in following way.

1. Open <tomcat-home>/conf/tomcat-users.xml


2. Add following entries or just uncomment and edit existing entries as shown below. 


<?xml version='1.0' encoding='utf-8'?>

<tomcat-users>
  <role rolename="manager"/>
  <role rolename="admin"/>
  <user username="tomcat" password="tomcat" roles="admin,manager"/>
</tomcat-users>

Note: manager must be added as a role to allow access to <Tomcat Manager> menu


Source: http://www.mkyong.com/tomcat/tomcat-default-administrator-password/ 


Remote debugging Tomcat 6 using Eclipse

With Tomcat 6 you could quite easily debug server side codes using Eclipse.

Step 1: What you fist have to do is, start the Tomcat using the following command.


          Tomcat6\bin>catalina.bat jpda start

Step 2: Next create a remote application configuration as shown in the following figure and run it.


Spring-Framework-3 and Hibernate-Framework-4 Integration: Common Exceptions and Solutions

Exception: java.lang.NoSuchMethodError: org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session

Spring has dropped HibernateDaoSupport/HibernateTemplate from their Hibernate4 support package.
So anyone of you who is using above classes with Hibernate4 will get a java.lang.NoSuchMethodError: org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session.
The simple solution for this is to, directly use hibernate classes instead of above mentioned Spring classes. This is illustrated in the following example.

Ex:
@Repository
public class UserDAOImpl implements UserDAO {

@Autowired
private SessionFactory sessionFactory;

public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}

@Transactional
public void saveUser(User user) {
sessionFactory.getCurrentSession().saveOrUpdate(user);
}

@Transactional
public List<User> listUsers() {
return sessionFactory.getCurrentSession().createCriteria(User.class).list();
}
}

Here @Transactional spring annotation added to the data base access methods for transaction handling. For example if a runtime exception is thrown in middle of a data base access operation the changes done within the transaction will be rolled back in the data base.
When using @Transactional one needs to add following jars to the lib folder and to the class path.

1. aopalliance.jar
2. spring-aop-3.2.1.jar

And should add following tags to the configuration file

1. For beans <beans xsi:schemaLocation="..."> property append following

 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-2.5.xsd
 http://www.springframework.org/schema/tx
 http://www.springframework.org/schema/tx/spring-tx-2.5.xsd

Ex:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-2.5.xsd
 http://www.springframework.org/schema/tx
 http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

2. Add following tags after the sessionFactory bean definition

<bean id="transactionManager"
          class="org.springframework.orm.hibernate4.HibernateTransactionManager">
          <property name="sessionFactory" ref="sessionFactory"/>
</bean>
       
<tx:annotation-driven transaction-manager="transactionManager" />
<context:annotation-config/>
<context:component-scan base-package="example" />


Note: Any miss configuration will throw an exception: No session found for current thread

Exception: java.lang.NoClassDefFoundError: org/hibernate/cache/CacheProvider

If you use org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean with Hibernate4,

Ex:
<bean id="sessionFactoryId" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

you will end up with a java.lang.NoClassDefFoundError: org/hibernate/cache/CacheProvider exception.
Reason for this is CacheProvider class is removed from Hibernate4. So you should use org.springframework.orm.hibernate4.LocalSessionFactoryBean instead.

EX:
<bean id="sessionFactoryId" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

Exception: java.lang.NoSuchFieldError: TRACE

This is due to a class confliction caused by log4j-1.2.8.jar. Removing it from the lib folder and class path will solve this issue.