-->
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.  [ 13 posts ] 
Author Message
 Post subject: Optimistic locking with CRC checking
PostPosted: Mon Aug 16, 2004 4:06 pm 
Beginner
Beginner

Joined: Mon Aug 09, 2004 3:35 pm
Posts: 22
Location: Minneapolis, MN, USA
Has anyone implemented or thought of implementing optimistic
locking with CRC checking? A quick search of the forums didn't
yield anything. I have the typical case where there are a lot of
legacy apps writing to the DB that no one is going to change.

It seems like it'd be pretty simple to do on top of Hibernate--just
a get with a lock while I computed the CRC--but I'd like to put it
inside of Hibernate. Has anyone on the Hibernate team considered
this as a feature before?

thanks,

robert


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 16, 2004 4:59 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Write a PropertyAccessor, and map it with the <version> tag.

Thats a cool application of PropertyAccessor, I might blog about this!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 16, 2004 5:05 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Actually, I'm wrong.


Explain to me where you want to keep the "old" CRC value when your objects are detached.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 16, 2004 5:07 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Couldn't you keep it in a property just like version? But that makes you miss the "increment" part of version I suppose.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 16, 2004 5:12 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Yes, if you kept it in a property, then all you would need is some way to "increment" version numbers with custom code. Currently VersionType only takes the old version number in the increment method, so some extra hook would be needed. (Its trivial, of course.)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 16, 2004 5:23 pm 
Beginner
Beginner

Joined: Mon Aug 09, 2004 3:35 pm
Posts: 22
Location: Minneapolis, MN, USA
gavin wrote:
Explain to me where you want to keep the "old" CRC value when your objects are detached.


I'm still considering options. I'd prefer to make it transparent to the
app, but proxies have their own little bits of baggage. It's either got
to go on the object or be associated with it in the cache, I suppose.

-r


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 16, 2004 5:45 pm 
Beginner
Beginner

Joined: Mon Aug 09, 2004 3:35 pm
Posts: 22
Location: Minneapolis, MN, USA
gavin wrote:
Yes, if you kept it in a property, then all you would need is some way to "increment" version numbers with custom code. Currently VersionType only takes the old version number in the increment method, so some extra hook would be needed. (Its trivial, of course.)


I'm still just getting my feet wet with Hibernate, but the heart
of the problem is that on update you have to go back to the DB
with a "select for update" to lock the row while you compute the
CRC of the current value in the DB and compare it to the original
value on the returning object. It doesn't really seem to mix and match
well with version.

I looked through the 2.1.6 code to track where version was handled and
it fits into the general flow quite easily. It would be tougher with a
CRC model since you couldn't batch for one thing.

The other difficulty would be to try and replicate version WRT
collections. As it is now, version operates exactly the way I'd expect
it to: versions are incremented when the set changes, but not when
a value of one of the members of the set changes. (Really nice job
there, in general). With CRCs you rely on locking to stop the race
condition between your read and the subsequent update. This gets
a little tricky with relationships.

My $.02 (USD),

-r


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 16, 2004 6:54 pm 
Regular
Regular

Joined: Fri Dec 12, 2003 2:09 pm
Posts: 84
Location: San Francisco, USA
Out of curiosity, why do you want to do CRC checking at the application layer?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 16, 2004 6:59 pm 
Beginner
Beginner

Joined: Mon Aug 09, 2004 3:35 pm
Posts: 22
Location: Minneapolis, MN, USA
hypernate wrote:
Out of curiosity, why do you want to do CRC checking at the application layer?


Where else could you put it? It's about the only place I have that sort
of control over. It would be best if I could tuck it away into the DB, but
that's not feasible in this case.

-r


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 16, 2004 7:16 pm 
Regular
Regular

Joined: Fri Dec 12, 2003 2:09 pm
Posts: 84
Location: San Francisco, USA
I mean, why don't you trust the network layer to communicate data correctly, your hard disks and RAM to store data reliably, and so on? Nowadays, pretty much every layer of the hardware stack implements data reliability safeguards.

I don't see the business/technical need for CRC data checks?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 16, 2004 11:28 pm 
Beginner
Beginner

Joined: Mon Aug 09, 2004 3:35 pm
Posts: 22
Location: Minneapolis, MN, USA
hypernate wrote:
I mean, why don't you trust the network layer to communicate data correctly, your hard disks and RAM to store data reliably, and so on? Nowadays, pretty much every layer of the hardware stack implements data reliability safeguards.


It's an not uncommon alternate implementation of optimistic locking.
I have the typical need for it on this project: lots of legacy applications
(Cobol, Uniface) that aren't going to get upgraded to make them
version-aware. So, you do something like this:

1. Serialize the object that you retrieve from the DB and compute
a CRC (or other checksum, MD5 if you really want to be certain) and
attach it to the outbound object.

2. At some point, you get the object back for an update, so you lock
the row that you're updating (e.g., select ... for update), retrieve a fresh
object from the DB, serialize it, and compute the checksum for the
current set of values.

3. If the checksum of the object you just retrieved matches the
checksum of the object you just got back for an update, write the
new values to the DB. If not, throw a stale data exception.

There are a lot of drawbacks to this method, but sometimes
circumstances force you to adopt the less-than-optimal solutions.

-r


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 17, 2004 11:53 am 
Beginner
Beginner

Joined: Mon Aug 09, 2004 3:35 pm
Posts: 22
Location: Minneapolis, MN, USA
rstreich wrote:
I'm still considering options. I'd prefer to make it transparent to the
app, but proxies have their own little bits of baggage. It's either got
to go on the object or be associated with it in the cache, I suppose.


I have to correct myself here: It'll have to be a visible property as
it'll have to be serializable.

So, there are only two key differences from version:

- It's a derived property, so it doesn't correspond to a column.

- You have to do a fresh read from the DB, with lock, prior to
update.

You could forgo the lock when you make a fresh read and just
accept the transaction isolation level set on the connection. MySQL
select ... for update only upgrades the isolation level to repeatable
read. I think it's the same for Oracle. Unless you set it to serializable,
you still have a race condition between the read and the write.

-r


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 17, 2004 2:57 pm 
Regular
Regular

Joined: Fri Dec 12, 2003 2:09 pm
Posts: 84
Location: San Francisco, USA
Interesting! I haven't encountered this before and appreciate the explanation.


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