-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Hibernate caching, what is the default setting?
PostPosted: Fri Aug 06, 2010 2:01 pm 
Newbie

Joined: Thu Jul 16, 2009 5:23 pm
Posts: 11
Using hibernate 3.3.2 on weblogic 10 with the javax.persistence.EntityManager interface. I'm configuring using persistence.xml.

I have a multi threaded application (using MDBs) that contains a counter column in one of my tables. Each thread will "select * from table where id = ? for update", read the count row, increment the count, and then "update table set count = newCountValue where id = ?".

The application works fine if there is only one thread, however when I run multiple threads, I am not getting the results that I expect.

For example if there are 2 threads each processes a single event, the final count should be 2. However, if one of the threads blocks on the select for update call, when this thread unblocks it reads a stale value of 0 for the count and incorrectly sets the count to 1.

This is the sequence of processing:
T1 - calls select for update
T2 - call select for update and blocks waiting for the locked row
T1 - reads 0, updates the value to 1 and commits
T2 - reads 0 (this is a stale value, it should read 1), updates the value to 1 (should be 2) and commits

I have looked at trying to set the following parameters in persistence.xml:
<property name="hibernate.cache.use_query_cache" value="false"/>
<property name="hibernate.cache.use_second_level_cache" value="false"/>
This did not help.

I have tried setting the query hint:
query.setHint("org.hibernate.cacheable", "false");
This did not help.

I have tried calling entityManager.flush() in various places. This did not help.

What is the default behavior of hibernate caching? How do you turn it off (I never want hibernate to cache any entity)? How can I guarantee that my query will go to the database to retrieve the data?

Thanks for any help you can offer.


Top
 Profile  
 
 Post subject: Re: Hibernate caching, what is the default setting?
PostPosted: Fri Aug 06, 2010 5:16 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
read the count row

what does it mean?

Quote:
What is the default behavior of hibernate caching? How do you turn it off (I never want hibernate to cache any entity)? How can I guarantee that my query will go to the database to retrieve the data?

the default for entity cache is disabled, for query cache is disabled, and even if you enable them globally you still have to enable each specific cache or query to be cachable.

I don't think your problem is related to caches, looks more an isolation problem of the database (which one?), or improper use of transactions.
Did you try performing the same through a SQL console?

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Hibernate caching, what is the default setting?
PostPosted: Mon Aug 09, 2010 8:15 am 
Newbie

Joined: Thu Jul 16, 2009 5:23 pm
Posts: 11
s.grinovero wrote:
Quote:
read the count row

what does it mean?


Sorry, that should be "read the count column", not the row.

s.grinovero wrote:
I don't think your problem is related to caches, looks more an isolation problem of the database (which one?), or improper use of transactions.


I'm using oracle 10, read committed isolation level. This is definitely something wrong with how I've configured hibernate. I believe I'm using the default settings. Just to make sure this was not a issue with the database, I replaced the hibernate calls with basic JDBC calls. Running with JDBC works as I expected, the second thread reads 1 and the final value of count is 2.

Any other thoughts?


Top
 Profile  
 
 Post subject: Re: Hibernate caching, what is the default setting?
PostPosted: Mon Aug 09, 2010 8:38 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
T1 - calls select for update
T2 - call select for update and blocks waiting for the locked row
T1 - reads 0, updates the value to 1 and commits
T2 - reads 0 (this is a stale value, it should read 1), updates the value to 1 (should be 2) and commits

How are the transactions?
it should be
T1 - begin
T1 - calls select for update
T1 - reads 0, updates the value to 1
T1 - commit

It's also possible that you didn't register the transaction manager with the container - using any container/application server?
did you see the chapter about running hibernate in a container in the reference documentation?

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Hibernate caching, what is the default setting?
PostPosted: Mon Aug 09, 2010 9:03 am 
Newbie

Joined: Thu Jul 16, 2009 5:23 pm
Posts: 11
I'm using container managed transactions. For example, in my MDB declaration, I have:
@TransactionManagement(javax.ejb.TransactionManagementType.CONTAINER)

Let me have a closer look at the hibernate in a container documentation.

Interestingly, I have found a workaround to the problem. I'm not sure what kind of side effects this will have on the application. If I call entityManager.clear() before my query, the application seems to have the desired outcome.

So the code works if I have this:

entityManager.clear()
select for update
update value


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.