Link to home
Start Free TrialLog in
Avatar of sandeep1984
sandeep1984Flag for India

asked on

Help in performance tuning with Hibernate

HI,

I am using Hibernate 3.1 with JDK 1.6 & Apache tomcat 6.
Database server is MySQL Community Server 5.1 Free Edition.
I am using Windows XP for development & planning to use OpenSolaris for deploymant
Below I have attached my Hibernate.cfg.xml file, HibernateUtil.java & an example using it.

My problem is my web application is behaving inconsistently with Hibernate unable to save data in some cases. If I restart the tomcat, the problem gets solved & occurs again after sometimes.

Please suggest me the changes required to tune the Hibernate performance. Number of simultaneous connections are expected to be around 500-1000 in live environment.
Also please suggest me the Best OS where I can deploy Hibernate application with a free Web/App server.

Thank You

Sandeep.
1. HibernateUtil.java :
 
import org.hibernate.*;
import org.hibernate.cfg.*;
 
public class HibernateUtil {
 
    private static final SessionFactory sessionFactory;
 
    static {
        try {
            String configfile = "/hibernate.cfg.xml";
            sessionFactory = new Configuration().configure(configfile).buildSessionFactory();
        } catch (Throwable ex) {
           throw new ExceptionInInitializerError(ex);
        }
    }
 
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}
 
2. hibernate.cfg.xm :
 
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
		<property name="connection.username">root</property>
		<property name="connection.url">jdbc:mysql://localhost:3306/myDB</property>
		<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="myeclipse.connection.profile">Sandy-MySql</property>
		<property name="connection.password">sandy</property>
		<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.show_sql">false</property>
		<property name="hibernate.connection.pool_size">8</property>
		<property name="hibernate.format_sql">false</property>
		<property name="hibernate.use_sql_comments">false</property>
		<property name="hibernate.generate_statistics">true</property>
		<property name="hibernate.order_updates">true</property>
		<property name="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
		<property name="hibernate.current_session_context_class">thread</property>
		<property name="hibernate.connection.release_mode">	after_transaction</property>
		<property name="hibernate.c3p0.max_size">10</property>
		<property name="hibernate.c3p0.min_size">2</property>
		<property name="hibernate.c3p0.timeout">1800</property>
		<property name="hibernate.c3p0.max_statements">100</property>
		<property name="hibernate.c3p0.idle_test_period">300</property>
		<property name="hibernate.c3p0.acquire_increment">2</property>
		<mapping
			resource="com/san/myproject/hibernate/Queries.xml" />
		<!-- Mapping Files Here-->
	</session-factory>
</hibernate-configuration>
 
3. Sample Class :
 
import org.hibernate.Query;
import org.hibernate.Session;
import com.sandy.myproject.util.HibernateUtil;
 
public class TestService {
	public void doService() throws Exception {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
		//Do the Transactio here
		session.getTransaction().commit();
		session.close();
	}
}

Open in new window

Avatar of mahome
mahome
Flag of Germany image

If you expect so much users, increase ConnectionPool. Try values between 100 and 500.
<property name="hibernate.connection.pool_size">100</property>
Do you get errors if hibernate is unable to save?
>>Also please suggest me the Best OS where I can deploy Hibernate application with a free Web/App server.
Every OS on which JDK is available is good, because Hibernate, Tomcat only requires Java.
Avatar of Mick Barry
your connection pool size needs to match the available mysql connections
but first you need to do some profiling and testing as to where your bottleneck is.
it may not even be the database, for example tomcat nay just be running our of connections.

Avatar of sandeep1984

ASKER

What is the difference between these two ways of getting a session.
HibernateUtil.getSessionFactory().getCurrentSession();
HibernateUtil.getSessionFactory().openSession();
first returns the 'current' session

the 2nd opens a new session

Which one I need to use here?
use the current session unless you have a specific reason to open a new one

Thank you.
What value should I set for
cache.provider_class
we use ehcache

Is it free?
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you.
Its a part of with Hibernate 3.1 distribution.
I will try with this.