Hi,
Did you load the document before trying to modify it?
In v5.2 the atomic guard is turned on by default and you have to load the document to modify the document while using this feature.
class TestObj
{
public string Id { get; set; }
public int Version { get; set; }
}
[Fact]
public async Task TestCase()
{
const string id = "TestObj0";
using var store = GetDocumentStore();
using (var session = store.OpenAsyncSession(new SessionOptions{TransactionMode = TransactionMode.ClusterWide}))
{
var testObj = new TestObj{Version = 1};
await session.StoreAsync(testObj, id);
await session.SaveChangesAsync();
}
using (var session = store.OpenAsyncSession(new SessionOptions { TransactionMode = TransactionMode.ClusterWide }))
{
await session.StoreAsync(new TestObj{Version = 2}, id);
await Assert.ThrowsAnyAsync<ConcurrencyException>(() => session.SaveChangesAsync());
}
using (var session = store.OpenAsyncSession(new SessionOptions { TransactionMode = TransactionMode.ClusterWide }))
{
var testObj = await session.LoadAsync<TestObj>(id);
testObj.Version = 3;
await session.StoreAsync(testObj, id);
await session.SaveChangesAsync();
}
}
Best regards,
Igal
--
You received this message because you are subscribed to the Google Groups "RavenDB - an awesome database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ravendb/b0f1c9ba-0c6d-4be5-847f-4e3a218e7214n%40googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "RavenDB - an awesome database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ravendb/b0f1c9ba-0c6d-4be5-847f-4e3a218e7214n%40googlegroups.com.
Oren Eini CEO / Hibernating Rhinos LTD
|
Hi,
I am adding here code example just to clarify the behavior and the possible cause of the issue.
The atomic guard relay on the compare exchange feature and handled on the server-side.
The exception you got means we tried to modify the compare exchange with the wrong index.
We get this information from the RAFT part of the document change vector.
From the exception, you sent you tried to store the document with no RAFT part.
Can happen:
TransactionMode.ClusterWide
and then you try to store it with TransactionMode.ClusterWide
(In this case, the compare exchange will remain with its index and that will cause the issue).class TestObj
{
public string Id { get; set; }
public string Prop { get; set; }
}
[Fact]
public async Task TestCase()
{
const string id = "testObjs/0";
using var store = GetDocumentStore();
using (var session = store.OpenAsyncSession(new SessionOptions{TransactionMode = TransactionMode.ClusterWide}))
{
await session.StoreAsync(new TestObj(), id);
await session.SaveChangesAsync();
}
using (var session = store.OpenAsyncSession(new SessionOptions { TransactionMode = TransactionMode.ClusterWide }))
{
var loaded = await session.LoadAsync<TestObj>(id);
loaded.Prop = "Changed2";
await session.SaveChangesAsync();
}
using (var session = store.OpenAsyncSession(new SessionOptions { TransactionMode = TransactionMode.ClusterWide }))
{
await session.StoreAsync(new TestObj{Prop = "Changed"}, id);
//Should load the
var e = await Assert.ThrowsAnyAsync<ConcurrencyException>(async () => await session.SaveChangesAsync());
Output.WriteLine(e.Message);
}
using (var session = store.OpenAsyncSession())
{
session.Delete(id);
await session.SaveChangesAsync();
}
using (var session = store.OpenAsyncSession(new SessionOptions { TransactionMode = TransactionMode.ClusterWide }))
{
await session.StoreAsync(new TestObj { Prop = "Changed2" }, id);
var e = await Assert.ThrowsAnyAsync<ConcurrencyException>(async () => await session.SaveChangesAsync());
Output.WriteLine(e.Message);
}
using (var session = store.OpenAsyncSession())
{
await session.StoreAsync(new TestObj { Prop = "Changed3" }, id);
await session.SaveChangesAsync();
}
using (var session = store.OpenAsyncSession(new SessionOptions { TransactionMode = TransactionMode.ClusterWide }))
{
var loaded = await session.LoadAsync<TestObj>(id);
loaded.Prop = "Changed2";
var e = await Assert.ThrowsAnyAsync<ConcurrencyException>(async () => await session.SaveChangesAsync());
Output.WriteLine(e.Message);
}
}
Best regards,
Igal
To view this discussion on the web visit https://groups.google.com/d/msgid/ravendb/6b11b6d9-4cba-46ed-9aaa-e5b66eab506en%40googlegroups.com.