Leo Nix
Leo Nix

Reputation: 2105

RavenDB: Raven.Abstractions.Exceptions.ConcurrencyException: A document with key: 'cache-fncwr10b' is currently created in another transaction

Issue context: Have a piece of code that's trying to add a new document to RavenDB in an NServiceBus message handler.

The code:

partial void HandleImplementation(SomeMessage message)
{
    PutDataIntoCache(message.CacheKey, message.Document);
}

void PutDataIntoCache(string key, Document document)
{
    using (var documentStore = new DocumentStore { Url = "http://localhost:8084" })
    {
            documentStore.Initialize();
            using (IDocumentSession session = documentStore.OpenSession())
            {
                using (var tx = new TransactionScope())
                {
                    session.Store(document, key);
                    session.SaveChanges();
                    tx.Complete();
                }
        }
    }
}

The error

I'm getting the following error:
Raven.Abstractions.Exceptions.ConcurrencyException: A document with key: 'cache-fncwr10b' is currently created in another transaction
   at Raven.Client.Connection.ServerClient.DirectBatch(IEnumerable`1 commandDatas, String operationUrl) in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Connection\ServerClient.cs:line 863
   at Raven.Client.Connection.ServerClient.<>c__DisplayClass56.<Batch>b__55(String u) in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Connection\ServerClient.cs:line 837
   at Raven.Client.Connection.ServerClient.TryOperation[T](Func`2 operation, String operationUrl, Boolean avoidThrowing, T& result) in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Connection\ServerClient.cs:line 222
   at Raven.Client.Connection.ServerClient.ExecuteWithReplication[T](String method, Func`2 operation) in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Connection\ServerClient.cs:line 192
   at Raven.Client.Connection.ServerClient.Batch(IEnumerable`1 commandDatas) in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Connection\ServerClient.cs:line 837
   at Raven.Client.Document.DocumentSession.SaveChanges() in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Document\DocumentSession.cs:line 441
   at CacheWarmer.Listener.Caching.UpdateCacheEntryProcessor.PutDataIntoCache(String key, ConfgDataModel confgDataModel) in UpdateCacheEntryProcessor.cs

The question Has anyone run across this? How did you solve it? What could be causing this?

Any helpful comments or tips will be greatly appreciated!

Upvotes: 3

Views: 2035

Answers (3)

Daniel Marbach
Daniel Marbach

Reputation: 2314

If you use NSB correctly you can remove almost everything from your spiked code and only write session.Store() on your injected document session. But you need to wire up an IManageUnitOfWork for that. TransactionScope will already be opened from NSB if you have transactional endpoints with DTC enabled.

Upvotes: 0

Ayende Rahien
Ayende Rahien

Reputation: 22956

Also, do NOT create a new DocumentStore on every call.

Upvotes: 4

Jonathan Matheus
Jonathan Matheus

Reputation: 1350

This error usually happens when you try to modify the same document from 2 different raven clients, or when you set the resourceManagerId to a different value across application restarts. I'm not seeing you setting the ResourceManagerId in the code that you provided, so it might be the first issue.

If you're going to modify the same raven database from multiple clients on the same machine (in your case NServiceBus endpoints), then you'll need to specify a resourceManagerId for each client (DocumentStore). You can specify this in the connection string or by Setting the ResourceManagerId property on the DocumentStore. This Id should be unique and persistent across restarts.

Upvotes: 1

Related Questions