• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1381
  • Last Modified:

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

0
sandeep1984
Asked:
sandeep1984
  • 5
  • 5
  • 3
1 Solution
 
mahomeCommented:
If you expect so much users, increase ConnectionPool. Try values between 100 and 500.
<property name="hibernate.connection.pool_size">100</property>
0
 
mahomeCommented:
Do you get errors if hibernate is unable to save?
0
 
mahomeCommented:
>>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.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
objectsCommented:
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.

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

the 2nd opens a new session

0
 
sandeep1984Author Commented:
Which one I need to use here?
0
 
objectsCommented:
use the current session unless you have a specific reason to open a new one

0
 
sandeep1984Author Commented:
Thank you.
What value should I set for
cache.provider_class
0
 
objectsCommented:
we use ehcache

0
 
sandeep1984Author Commented:
Is it free?
0
 
objectsCommented:
0
 
sandeep1984Author Commented:
Thank you.
Its a part of with Hibernate 3.1 distribution.
I will try with this.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now