Showing posts with label Hibernate. Show all posts
Showing posts with label Hibernate. Show all posts

Thursday, December 27, 2007

Hibernate .vs. Pure JDBC

There is a wrong assumption that Hibernate is slower then JDBC, this is not true.
For the tests that I have done I had found that Hibernate performance are not less then Pure JDBC.

I had compared the performance between Hibernate and Pure JDBC for writing 10,000 rows to the DB table, The results are really surprising.

The test run on a single thread which write in a loop the rows in bulk update, the JDBC used batch update processing with prepared statement, the Hibernate run on a single session which flush the inserts to the DB every 100 rows (Bulk Updates).

I had worked with Hibernate 3.0 on a regular Pentium Pentium 4 2.66GHz with 2GB RAM, The DB was remote Oracle 9 DB.

Well are are the surprising, Hibernate and Pure JDBC results are completely the same !!!
Hibernate doesn't have a huge overhead as you expect it to have.

For the results I manage to write 10,000 rows to the DB in 1.5 Seconds !!!

Here are some tips for improve your Hibernate performance are:

  • Try to reduce the number of open and close of hibernate sessions
  • Using Hibernate bulk updates were possible
  • When using Hibernate bulk updates try to separate the SQL Insert/Update/Delete statements to groups, the DB would process this groups faster (For example 20 SQL inserts then 40 SQL updates and then 25 SQL delete statements)

Wednesday, December 12, 2007

Mapping Hibernate to DB View

There is a problem to map POJO to DB View using Hibernate if you are using also HBM2DLL.
In this case the HBM2DLL would create for each HBM mapping a corresponsing DB table.

To solve the problem -

  • Write HBM as usual
  • Add deletion of the DB table as follow:


DROP TABLE MY_TABLE;


  • Create the view as follow (also in databse object tag)
CREATE VIEW MY_VIEW AS SELECT ....;

Hibernate would write data and read data now with the DB view.

Monday, December 10, 2007

Improve Hibernate performance using Bulk

If your application needs to run under heavy stress you can improve a lot the performance using hibernate by using bulk updates.

Hibernate allow you to to write bulk of several SQL insert statements at one time,
To do it you need to open an hibernate session, write a set of sql insert statements or a set of sql update statements (don't mix the insert and the update statements togther), and call session.flush() this would use the DB capabilities to write bulk of sql statments in efficent way.

Hibernate underneath use something like JDBC Batch to implement the bulk updates.

I succeed to write in this way 10,000 rows in 1.5 seconds in Oracle DB Table on a simple Pentium machine.

Sunday, December 9, 2007

Is Hibernate Portable?

Hibernate is considered to be portable framework in which you can move from one databse to another database without the need to change the code at all, Does is it true?

It is true that most of the code and the HBM files (or anotations) would remain the same when moving from one database to another database, however there are some exceptional cases.

For example consider the following simple HQL query:
select from USERS where NAME=?

If the column NAME is nullable in the databse and you would pass null value as parameter then in MySQL it would return all the rows where NAME is NULL, However in Oracle it wouldn't return any row at all, the reason for it is that in Oracle
NULL==NULL is false.

There are other much more exceptional cases, please comment if you need other examples.

So the answer for the question is Hibernate Portable is Yes for 90% of code but No for 10% other perecent of code.

Saturday, December 8, 2007

When to flush session in hibernate?

I heard once an hibernate expert says that there is no need in any way to flush the hibernate session, According to this expert the hibernate knows better then the developer when to flush the session and when not, For example if the developer insert data to the DB several times and then read select the same row from the DB then Hibernate would just flush before the select statement.

However this is not true, there are cases when flush the session is mandatory.

For example consider the following case: In a method you insert some data to a DB table, then you send a JMS message (asynchronously) which invoke MDB which need to select the same data (in a different hibernate session) which was insert to the DB just before the JMS message,

In this case the data my not flush yet by the hibernate before the MDB select the same data,
In this case it is required to flush the session before sending the JMS message.

Links

 
RSS Feeds Submission Directory