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.  [ 9 posts ] 
Author Message
 Post subject: question about second level cache and transactions
PostPosted: Tue Apr 20, 2004 9:40 am 
Newbie

Joined: Thu Apr 08, 2004 7:17 am
Posts: 10
Hi,

we are developing a J2EE application using Orion, replacing CMT/CMP with hibernate as persistence layer.

Im wondering witch second level cache to use. Im not sure whats the meaning of transactional in that context is.

Lets take a look at EHCache for example.
I have a transaction and change an object that I retrieved from the cache in that transaction. Before commit or rollback another thread retrieves the same object from cache. Does this thread get the original or the changed values?

If it gets the changed values, is JBoss TreeCache the only cache for hibernate that permits this?

Tanks in advance for your help.

Alex


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 20, 2004 1:46 pm 
Regular
Regular

Joined: Wed Mar 03, 2004 9:38 am
Posts: 70
Disclaimer: I haven't looked at the ehcache src, so I'm just speculating.

Now, if you enable debug level logging for the caching system and SQL, you will see messages in the following order (using ehcache):

Code:
13:18:03,582 DEBUG ReadWriteCache:106 - Invalidating: 941
13:18:03,586 DEBUG SQL:237 - update blah blah
13:18:03,746 DEBUG ReadWriteCache:227 - Updating: 941
13:18:03,749 DEBUG ReadWriteCache:243 - Updated: 941


If nothing else, this strongly suggests that the instance of the object in the cache is NOT the same instance that you're manipulating. Rather, what happens is that when you commit your transaction the instance in the cache is first marked invalid, then the actual db transaction takes place, and if it's successful the instance in the cache is updated by copying the properties. This, in effect, makes ehcache behave pretty much according as READ COMMITTED isolation level, which is the default isolation level for most databases.

However, I suspect that ehcache doesn't ensure that the cache updates are atomic if you use nonstrict-read-write, i.e. if you update many rows in a single db transaction the updated cache objects are available for other threads immediately after update, and not after all the cache objects in the transaction are updated. Or can anybody confirm that I'm wrong here (javadocs at least mention that a NonstrictReadWriteCache never locks the cache)?

If you use read-write, I think that ehcache locks the cached objects, which should make sure that cache updates for a single db transaction are also atomic.

Now, the meaning of transactional in the context of TreeCache probably means that TreeCache participates in the same transaction as the actual DB operations (it depends on JTA). This certainly ensures that updating many cache objects in a single transaction is an atomic operation. Also, I'm quite sure Treecache uses some 2-phase commit protocol to ensure that cache updates are successfully propagated to all the nodes in a cluster.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 20, 2004 7:23 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
A quick rule of thumb is, transcational concurrency strategy gives you the equivalent to repeatable read isolation in a managed environment (Appserver). read-write gives you read-committed isolation basically. However always be very careful when using a cache in cases where you need transaction isolation.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 20, 2004 8:13 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
Where can we get more information on caching in Hibernate?
Especially regarding what effect implementing caching will have on transaction isolation for our applications.

The manual's fairly vague in this area, do we just have to read the code of Hibernate and the cache implementation we choose (probably EHCache) and hope that we interpret it properly?

Our specific environment involves a standalone Tomcat instance, so no appserver/JTA so TreeCache can't help us.

We must have an isolation level of at least read committed.

The manual says (for "read-write" usage):
Quote:
In other environments, you should ensure that the transaction is completed when Session.close() or Session.disconnect() is called.

Does this mean that if we can make this guarantee about closing the session that we can assume that a competent cache implementation (ie. EHCache) will give us an isolation level of read committed?

_________________
Cheers,
Shorn.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 20, 2004 9:53 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
You can get us onsite for performance tuning or direct support. See my signature.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 20, 2004 10:43 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
christian wrote:
You can get us onsite for performance tuning or direct support. See my signature.

We're not doing performace tuning yet, we're not even doing proper load testing yet (hence, we're not switching on 2nd-level caching yet).

We just want to know the transaction isolation repercussions of switching on 2nd-level caching in Hibernate so we can know whether further investigation (and possible expenditure on consultants) is warranted.

Or do we just have to look at the code and hope we understand?

For us, if read-committed isolation level can not be guaranteed (for at least some operations), then it can be dismissed as a viable possibility.

_________________
Cheers,
Shorn.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 20, 2004 10:51 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
For each class or association, pick a cache concurrency strategy:

transactional: Available in a managed environment only, it guarantees full transactional isolation up to repeatable read, if required.

read-write: This strategy maintains read-committed isolation, using a timestamping mechanism and is available only in non-clustered environments.

nonstrict-read-write: Makes no guarantee of consistency between the cache and the database, uses a timeout/expiration mechanism.

read-only: A concurrency strategy suitable for data which never changes.

Then, chose a chache provider that supports your strategies and configure everything.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 20, 2004 11:02 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
Did you just type all that out, or is it from some documentation we can read? :)

christian wrote:
read-write: This strategy maintains read-committed isolation, using a timestamping mechanism and is available only in non-clustered environments.


So, if I'm understanding correctly; if we stick to usages of "read-only" & "read-write" and use EHCache - then we're probably Okay even in a non-managed environment (ie. a basic Tomcat instance).

Or to put it a different way:
Given the above, if our DB is configured for at least read committed isolation level, then Hibernate/EHCache 2nd-level caching will also provide a read committed level of isolation for transactions.

_________________
Cheers,
Shorn.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 20, 2004 11:04 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
It is from Hibernate in Action, available real soon[tm].

Your conclusion is correct.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.