Reputation: 869
I am going through the popular Pro ASP.NET MVC 5 book and building the Sports Store application, but I am using RavenDb instead of Entity Framework.
Initially, I created the Product records in Raven through the local API (not through code). I gave the records a manual Id and created the json fields and values for each product - 9 in total. I then wrote the parts of the application that loaded these products and everything worked totally fine. The data returned exactly as I expected every time.
However, when I got to the part of the application that allowed users to create new records through the MVC interface, I got a crash when calling the SaveChanges() function on IDocumentSession.
This is the full text of the error:
Server Error in '/' Application.
PUT attempted on document 'products/9' using a non current etag
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: Raven.Abstractions.Exceptions.ConcurrencyException: PUT attempted on document 'products/9' using a non current etag
This is the full text of the Save function I am calling:
private readonly IDocumentSession _session;
public void Save(Product product)
{
_session.Store(product);
_session.SaveChanges();
}
_session is initialized in the repository constructor like this:
public ProductRepository()
{
_session = RavenDbStoreSingleton.DocumentStore.OpenSession();
}
And this all interacts with a singleton for DocumentStore:
public class RavenDbStoreSingleton
{
private static readonly Lazy<IDocumentStore> Store = new Lazy<IDocumentStore>(CreateStore);
public static IDocumentStore DocumentStore => Store.Value;
private static IDocumentStore CreateStore()
{
IDocumentStore store = new DocumentStore()
{
Url = "http://localhost:8080",
DefaultDatabase = "SportsStore"
}.Initialize();
return store;
}
}
So my guess is that my manually entering ALL of the first records and then trying to save new records somehow caused this error, but I'm not sure why Raven can load the Products but can't seem to save without hitting conflicting Ids. I even went and increased the Hilo for the Products (which is the only document type) from 32 to 64, but that didn't affect anything.
Update: I noticed that each time I got this error, it conflicted on a higher Id than it did previously. I was able to keep attempting to save until the error no longer occurred, at which point the Id generated was Products/65, which makes sense as I had modified the Hilo from 32 to 64. It did not try to generate Products/10.
However, I don't understand why modifying the Hilo halfway through my 9 crashes didn't make Raven start generating at Products/65 and instead it kept trying to generate already taken Ids.
Upvotes: 3
Views: 1674
Reputation: 22956
The reason why it did it was that it was caching the hilo value, and until it run out of the hilo range, it had no reason to request a new range. The hilo doesn't go to the server on every request.
Upvotes: 2