Bob Horn
Bob Horn

Reputation: 34297

Referential Integrity with RavenDB

I know the title of this post has to make some people cringe. I've been using RavenDB since December 2011, and I've come to realize that I've modeled my data in a way that doesn't fit with using a document DB. First, I totally get that. Second, I'm hoping there is a way, given my current situation, to make the scenario below work correctly.

Scenario: I want to delete a certain Foo document in the database. However, I don't want to delete it if that document is being referenced by another document. So, I've done this:

public void Delete(Foo foo)
{
    VerifyFooNotUsedByBar(foo);  // Make sure no bars reference this foo

    VerifyFooNotUsedBySnuh(foo);  // Make sure no snuhs reference this foo

    // What happens if some other user causes this foo to be referenced
    // by a bar, right now?

    new GenericData().Delete(foo);
}

This works, however... There is a chance that foo could be referenced by something else, after I do the checks for them. Is there a way to lock other changes while until this entire method completes?

I understand if the answer is: "Nope. You screwed yourself by using a document DB this way." I'm just hoping there is a way around my current issue.

Upvotes: 3

Views: 363

Answers (1)

Ayende Rahien
Ayende Rahien

Reputation: 22956

Bob, Not really, no. Even if you had a transaction around this, it won't lock. What you CAN do is to make sure that the references would be maintained in the metadata. So when I am referencing Foo, I need to update Foo's metadata to tell it that I am doing so. That would cause a single point of transactional truth.

Upvotes: 2

Related Questions