Transactions behaves differently than I would expect

In this code:

var trans = _connection.StartTransactionAsync(StreamName, seqNumber).Result;

try

{

trans.WriteAsync(evtData).Wait();

trans.CommitAsync().Wait();

}

catch

{

trans.Rollback();

throw;

}

I might get a WrongExpectedVersionException.

My expectation would be that this Exception would be thrown at the WriteAsync(…).Wait() call allowing me to call Rollback in the catch block.

But contrary to my expectations the exception is thrown at CommitAsync().Wait() - and when I then call Rollback I get a new exception telling me that the transaction is already committed ?!

This is not true because the event does not arrive at the stream.

First of all it is weird that the transaction is registered as committed, secondly what is the use of Rollback in the above. It can never be called in the above code, because Write will never fail, only commit and commit disallows Rollback,

So what is the correct use of transactions ? The documentation does not tell me. It only gives me the method names and parameters.

There is also a method called ContinueTransaction which is a total mystery to me :slight_smile: I expect there is something I am missing - a simple code sample might help.

Regards Jesper

The already committed is just a client exception, it could use better wording.

Some things with expected version cannot be checked until commit.

ContinueTransaction if you look each transaction has a uuid
identifier. I can start a transaction, lose power ... get restarted
and continue a transaction I was previously working with if I remember
its uuid and pass to continuetransaction.

OK. I get it.It makes sense that it is only possible to check ExpectedVersion at commit time.

You are right the Exception thrown at Rollback could use better wording.

But was is the actual use of Rollback in this ? Is it ever necessary to call it ? Is it just a fancy way of disposing the transaction object ?

I.e. would this code - the same as before but with no Rollback - be OK ?

var trans = _connection.StartTransactionAsync(StreamName, seqNumber).Result;

trans.WriteAsync(evtData).Wait();

trans.CommitAsync().Wait();