0

I migrate from OpenJPA 1.2 to Hiberante 4.0
I'm using TimesTen DB

I'm doing a native query to get id's of object's that I need , and then perform find on each on of them. In OpenJPA instead of find I used findCache() method and if it return null I use the find() method , In hibernate I used only the find() method.

I performed this operation on the same DB.

after running couple of test I saw that the performance of OpenJPA is far better.

I printed the statistics of hibernate session ( after querying and finding the same object's) and saw that the hit\miss count to the first level cache is always 0. while the OpenJPA is clearly reaching it's cache by fetching object's with the findCache method.

How can I improve the performance of find in Hibernate ? I suspect it referred to the difference in the first level cache implementation of this tools.

another fact: I use the same EntityManager for the application run time ( I need to minimize the cost of creating of an EntityManager - my app is soft real time )

thanks.

eitann
  • 1,203
  • 10
  • 23

2 Answers2

0

Firstly, why don't you just retrieve the full objects instead of the id. One select statement to retrieve a number of objects is many magnitude times faster than retrieving each item individually.

Secondly, you likely need a second level cache for hibernate. The first level cache is mostly applicable within each session.

drone.ah
  • 1,135
  • 14
  • 28
  • I tested the retrieve of the full object instead of the id , and it was almost the same , I fetched each because of the findCache in openjpa before migrating to hibernate. I most say also that I use the same entityManager for all the apllication runtime – eitann Oct 31 '12 at 07:37
  • For a few objects, the time taken may not be that different depending on the hardware. The time disparity will increase (very rapidly) as the number of objects being retrieved increases. Is there a reason for retrieving them individually? If not, I would definitely go for loading objects. For the DB, the cursor has to find the rows (the most expensive part) to retrieve the id. It is negligible extra effort to pick up the rest of the fields. When you do another retrieve, it has to go and find that row again. There are other consideration as well. – drone.ah Oct 31 '12 at 07:45
  • In response to using the same entity manager: Since you are using the Entity Manager, the hibernate session management is out of your control. I am not entirely sure but my experience is that the first level cache is more or less valid only within a session. It is entirely likely that the EntityManager manages the sessions in such a way that the first level cache becomes largely unused in your context. A second level cache should resolve this. – drone.ah Oct 31 '12 at 07:49
0

The first level cache in Hibernate corresponds to the session. So if the session has not yet loaded a given object, it will be a miss. You need to enable second level cache to be able to cache an object by id across sessions.

Check out the reference documentation for more info http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html_single/#performance-cache