This post originated from an RSS feed registered with .NET Buzz
by Sean McCormack.
Original Post: Need Help - Problem with Transactions and Object Caching
Feed Title: Vini Vidi Vici - Sean McCormack's Blog
Feed URL: http://smccormack.blogs.com/adapdev/SyndicationService.asmx/GetRss
Feed Description: Sean McCormack's Blog, focusing on various aspects of .NET development, open source projects, recommended books and tools
I'm back from the dead. :-) It's been a hectic couple of weeks...
So, I've encountered an interesting problem and haven't figured out how to get around
it. I was hoping one of you gifted readers could help me! Here's the problem:
I have a new ORM framework that I'm working on called Elementary, which is very similar
to nHibernate and provides some of the advanced features people have asked for in
the current Codus DAO Framework (Elementary will be integrated with the Codus generator).
One of the more advanced features that Elementary provides is object-caching.
So, when you request a record from the database, the result is cached. Next-time
you request the same record, the cached object is returned, saving you a full database
trip. It provides for some marked performance improvements.
Here's a typical scenario retrieve and update scenario with caching turned on:
1. A database record is retrieved and turned into an Employee object (Version A).
A copy of the object is put in the cache.
2. The user modifies the record and saves it (Version B).
3. The record is updated in the database and the latest version of the object is put
in the cache (Version B).
4. Someone else requests the same record. Version B is returned from the cache.
All this works fine and dandy until you introduce transactions. Here's the same
scenario, but with a failed transaction:
1. A transaction is opened.
2. A database record is retrieved and turned into an Employee object (Version A).
A copy of the object is put in the cache.
3. The user modifies the record and saves it (Version B).
4. The record is updated in the database and the latest version of the object is put
in the cache (Version B).
5. The program rolls back the transaction. The record in the database, which
is Version B, is rolled back to Version A.
5. Someone else requests the same record. Version B is returned from the cache.
Ahh haa! A problem. The database has correctly rolled back the transaction
and restored the database record back to the original value. But, during the
update call, the cache was replaced with Version B and is unaware of the rollback.
So, the question is how to you deal with this? Is there a way to hook into a
COM+ transaction and get notified when a rollback occurs...such as an OnAbort event?
Any thoughts?