I'm trying to figure out how the CommitAsync method in nhibernate works in conjunction with SaveAsync, UpdateAsync, & DeleteAsync
My question is: say i call SaveAsync, or UpdateAsync multiple times without awaiting them. Then sometime later I call CommitAsync. Will CommitAsync wait for all of the calls to SaveAsync and UpdateAsync to finish? Or do i have to await them before calling CommitAsync?
I Created the test below and tried saving multiple users without awaiting them then tried to commit using CommitAsync to see if any would be missing from the DB or if the count would be off but it seems to work. However, every example I've seen online awaits all of their async CRUD operations before committing.
[TestFixture]
public class AsyncTests
{
[Test]
public async Task TestAsync()
{
var sessionSource = new SessionSource();
int saveCount = 100000;
using (var session = sessionSource.CreateSession())
{
using (var t = session.BeginTransaction())
{
SaveMany(saveCount, session);
await t.CommitAsync();
}
}
int count = 0;
using (var sessoin = sessionSource.CreateSession())
{
count = sessoin.Query<User>().Count();
}
Assert.That(count, Is.EqualTo(saveCount));
}
public void SaveMany(int count, ISession session)
{
for (var i = 0; i < count; i++)
{
Save(session);
}
}
private Task<object> Save(ISession session)
{
return session.SaveAsync(new User()
{
Guid = Guid.NewGuid(),
FirstName = "saveasync",
MiddleName = "middle",
LastName = "last"
});
}
}