Multi-statement transactions using query API

How can I execute a transaction of multiple DML statements? I prefer the approach of “BEGIN TRANSACTION”, then executing statements, and in the end “COMMIT”. But, how can I send the txid retrieved from the begin statement and send it in the following DML statements? (I know how to retrieve the txid, but I’m missing the part where I can send it as a param to the next execution)

I’m using java client 3.5.3

Hi @farespnx

The only supported way in the SDKs is to use the transactions API (Using Couchbase Transactions | Couchbase Docs) rather than BEGIN TRANSACTION directly.

Note this API is doing much more than just wrapping BEGIN TRANSACTION. It adds a lot of error handling and retry logic, to make sure your transaction continues to work robustly in cases such as write-write conflicts with other transactions.

There is no way of using BEGIN TRANSACTION directly through the SDK that will work with multiple query nodes. It is possible to send txid using QueryOptions.raw() - but you also need to send each request in the transaction to the same query node. You could do both using REST directly - but for the reasons above, I would strongly recommend using the SDK API instead.

1 Like

Thanks for the clarification.