CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Rod Paddock


Lifting the Skirt…Stored Procedures and Change Tracking

Recently I’ve been involved in an online discussion related to data update techniques. Part of the discussion turned to the issue of data updates and change tracking. A question came up:

>1) The SP receives and updates all fields every time.
>2) Contention management is via a pessimistic locking mechanism. Effectively you lock the entire row/s when a user enters edit mode.
>3) Therefore you don't need dataset/entity change tracking.

My answers:1) Yes. We update all columns.
2) True. Yup we lock the row or in the case of a complex entity we lock the header row which implies locks on the child tables.
3) Yes. That is true we don’t need these mechanisms.

We do use a lock table that contains table/entity/process name, key, user, date, lock notes, etc....

Long lunch syndrome or dead workstation is done via the age of the lock.

Old locks can be overridden, in the case of the dead workstation the same user can repossess there lock.

We generally have a clean locks routine incase of bugs or buildup.

The reason...When we started doing database work (even in the foxpro days) we used the same approach. So we did our best to take it to the SQL world and it has seemed to work OK for us.

We also have the ability to turn locks off as well. In a lot (not all) of web apps we don’t worry about contention as much as the owner of the data is the one manipulating it. Like in amazon the customer owns the record until a certain point and they are allowed to make changes to their order/customer info/etc....
 

Some background… we do ALL database access via Stored Procedures. We have our own custom CRUD class that does all of this via a common stored procedure API.

Our update transactions update all columns regardless of what changed.

We have our own semaphore based locking system.

So just like in that forum I am lifting the skirt here as well and would like to see how the CodeBetter world at large does this:

How do you handle updates?

How do you handle conflict checking and resolution?

How do Nhibernate or any  other data access tools do this?

 

 



Comments

cmyers said:

One of the projects we're working on required an aggressive locking scheme similar to the one mentioned above. Even worse, we had versioned rows (i.e. Invoice and InvoiceVersion, OTM) and we have to implement the locking mechanism AS WELL AS the latest-version-detection.

We're using NHibernate, but that's actually not relevant since the requirement is really a shared resource problem and the shared resource, in this case, happens to be the database.

NHibernate actually has a nifty row version/collision-detection system built in, but it was only for single-row tracking and not for a multi-row historical-type versioning.

We've avoided Sprocs at all cost except when interfacing with 'external' databases or linked servers (don't ask) in an effort to isolate the app and NHibernate from any nasty dependency knowledge. So far, some of the biggest problems we've had is in the stored procs due to duplication of logic, the procedure cache in SQL server getting hosed up because if you have anything more complicated than a SELECT/UPDATE/INSERT/DELETE in a proc it ends up recompiling often, etc, etc.

It's nice knowing that our most complicated and critical piece of code (the locking and versioning mechanism) is handled in one piece of .NET code that's well tested and high isolated.

Sorry to vent and dump on stored procs. I grew up in a VB6 Windows DNA family and so I cut my teeth on heavy stored proc usage and I don't think there's ever been a project where managing the gagillion stored procs didn't turn into a unmaintainable nightmare mess 2 weeks after release.

# April 24, 2007 10:18 PM

cmyers said:

Sorry, I meant:

"... ever been a project that I've ever seen ..."

I'm sure there are some out there that are doing it quite well and they have "IT" figured out (whatever "IT" is) but I'm not convinced that these types of projects are in the majority.

# April 24, 2007 10:20 PM

Eric Wise said:

I'm lucky enough to work in an environment where last in wins so locking isn't all that much of a concern for my current projects.

On updates, we do update the whole row, but we have insert/update/delete triggers that take an XML snapshot of the before and after image and writes it to an audit log comprised of auditID, tablename, keyid, beforexml, afterxml.

# April 24, 2007 10:33 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

Our Sponsors