Transactions with YCQL using C#

Hi there,

I’m using the C# client library at the following page:

https://docs.yugabyte.com/latest/develop/build-apps/csharp/

How does one use transactions with that library against YugaByte?

I’ve looked through the documentation and don’t see an example.

hi @Exocomp

Through the YCQL API, we support a limited form of multi-row/multi-table transactions using the

BEGIN TRANSACTION
  stmt1;
  stmt2;
END TRANSACTION; 

like syntax.

See Develop applications | YugabyteDB Docs.

While the examples in the above link use Java, it would be pretty similar in C# as well as the statement is handled by the server-side (so the usual prepare/bind/execute model will work just fine).

NOTE: the BEGIN … END isn’t as general as flexible as the say an anonymous PL/SQL or PLPGSQL like syntax. So you CANNOT intersperse arbitrary control flow (while loops, if () then … else … ) etc.

If you need more flexible transaction control, you can do multi-step client-controlled transactions through our YSQL API (which is currently in Beta).

Would love to learn a bit more about your use case (if you are open to it) and understand how you are planning to use transactions.

regards,
Kannan

2 Likes

@kannan

Thanks for the link that helps.

I’m currently using a batch statement to insert two records, for ex:

var batch = new BatchStatement()
                .Add(msg1.Bind(fields))
                .Add(msg2.Bind(fields));

The two records are related so if the second fails to add for any reason then the first should rollback. Which is why I wanted to use a transaction, am I right to use it in that using a batch statement there is a possibility the second could fail?

I think the example you provided will work fine for my needs. Sounds like I need to create a prepared statement with the begin/end and the two statements together and then just execute it.

That is correct @exocomp. You need to put the two statements in a single BEGIN … END statement and then just execute it (and you can still use the Bind (?) placeholders to avoid recompiling the statements each time).