?
Solved

Help in performance tuning with Hibernate

Posted on 2009-02-09
13
Medium Priority
?
1,353 Views
Last Modified: 2013-12-02
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
Comment
Question by:sandeep1984
  • 5
  • 5
  • 3
13 Comments
 
LVL 10

Expert Comment

by:mahome
ID: 23598244
If you expect so much users, increase ConnectionPool. Try values between 100 and 500.
<property name="hibernate.connection.pool_size">100</property>
0
 
LVL 10

Expert Comment

by:mahome
ID: 23598247
Do you get errors if hibernate is unable to save?
0
 
LVL 10

Expert Comment

by:mahome
ID: 23598263
>>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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 92

Expert Comment

by:objects
ID: 23598270
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
 
LVL 2

Author Comment

by:sandeep1984
ID: 23608143
What is the difference between these two ways of getting a session.
HibernateUtil.getSessionFactory().getCurrentSession();
HibernateUtil.getSessionFactory().openSession();
0
 
LVL 92

Expert Comment

by:objects
ID: 23608153
first returns the 'current' session

the 2nd opens a new session

0
 
LVL 2

Author Comment

by:sandeep1984
ID: 23608165
Which one I need to use here?
0
 
LVL 92

Expert Comment

by:objects
ID: 23608189
use the current session unless you have a specific reason to open a new one

0
 
LVL 2

Author Comment

by:sandeep1984
ID: 23609275
Thank you.
What value should I set for
cache.provider_class
0
 
LVL 92

Expert Comment

by:objects
ID: 23609304
we use ehcache

0
 
LVL 2

Author Comment

by:sandeep1984
ID: 23619057
Is it free?
0
 
LVL 92

Accepted Solution

by:
objects earned 1500 total points
ID: 23619061
0
 
LVL 2

Author Comment

by:sandeep1984
ID: 23619081
Thank you.
Its a part of with Hibernate 3.1 distribution.
I will try with this.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month15 days, 8 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question