Thursday, June 7, 2007

Comparing Hibernate and JDBC (time issues)

I used JFluid(Performance Analysis tool) for my web application
Contact management.
The product uses Hibernate to interact with the database.
As an example I will take Hibernate methods and some statistics
to compare it with simple JDBC.

On the console when I started the server the output was this.

It has all the methods that the tomcat needs to start up. The profiling is perfect. It has four columns. The first column tells about the method name. The second one tells about the % of total time. The third one tells about the time in absolute terms and fourth one tells about the invocation time in absolute terms.

Now coming on the actual implementation.

In this picture one thing that is worth noting that the hibernate internally does not take as much of the time as the apache Catalina loader and other apache functionalities. Even the so called expensive process of creation of sessionFactory (87 ms this takes into account the time taken to read the configuration file and the time taken to build the factory.) taking no time in comparison to the server methods. But it takes comparatively higher times in comparison to other methods like getNamedQuery ( double the time than this 54ms ) and other methods like getterMehods (34 ms) still lesser time.

Now coming on to Hibernate specific methods.

We will divide the work of hibernate in three different categories.

· Loading of Configuration . It will include reading from the XML file, loading the mapping and getting the methods to deliver the result.

· Interaction with the database. It will include the creation of connection with the sql and the time taken to query and creation of tables.

· Displaying the result.

Before we take a dip into the categories I would like to say that the first category took around 0.75 times the time taken by second category. This can change drastically if we increase the number of users. Though the schema update time won’t change. The third category is not even in contention.

Among the categories that are listed above the time taken by the different category is

Coming onto the first category.

In the first category when we take out the methods those took the maximum amount of time are listed in the next paragraph.

The init() method of org.hibernate.cfg.Configuration() which is used to upload the configuration given in the hibernate.cfg.xml takes 2.9 ms. The add method of the same class takes 13.7 ms. The building of factory takes 5.62 ms and creationMapping() took 1.32 second. Rest all the processes took less than 1 ms. Creation of session(0.012ms) out of sessionFactory did not take much time ( as per the expectation., session is lightweight object) . So putting the creation of sessionFactory in the Plugin is the step in the right direction.

Moving onto the second category.

The second category mainly consists of one time process of dialect upload , creation of Connection and multiple time processes of query translation, sqlgenerator from HQL and fetching of data from the tables.

One time process of connection creation takes in total of 23 ms. The amount of time is spent in dialect upload ( that is equivalent of loading the driver in JDBC) is 43ms. Which compares badly with the direct JDBC Driver loading. Even the creation of connection takes more time than the usual JDBC connection creation.

This statistics can put Hibernate lover to think once again for the application where the time is important parameter. The other big demerit of Hibernate is the translation of HQL to sql that also takes 12 ms which can be saved in case of JDBC. Fetching data once the sql has been generated would take the same amount of time.


Anonymous said...

Oi, achei teu blog pelo google tá bem interessante gostei desse post. Quando der dá uma passada pelo meu blog, é sobre camisetas personalizadas, mostra passo a passo como criar uma camiseta personalizada bem maneira. Até mais.

Anonymous said...

Dear Kanishka,

I came across this blog of yours where you written extensively about software development. Sysomos Inc, a spin-off company from the University of Toronto, is currently looking for developers to build social media monitoring platform. We are interested in a small team of highly skilled engineers. I see individuals such as yourself as a good match. More information is available on

I am currently in India for next two weeks, and we can discuss further if you are interested. Let me know by emailing me at nilesh AT

PS: Sorry, I was not able to get your email address, so I had to spam your comments page. You may delete this comment once you have read it.

Co-founder, Sysomos Inc.

Anonymous said...

Gostei muito desse post e seu blog é muito interessante, vou passar por aqui sempre =) Depois dá uma passada lá no meu site, que é sobre o CresceNet, espero que goste. O endereço dele é . Um abraço.