Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transaction on Graph Cosmos DB

I have two queries in gremlin which first one is adding a vertex, and the second one is adding an edge between the added vertex and an existed vertex in the graph.

string query1 = $"g.addV('session').property('id','{session_id}').property('initialState', 'start')";
string query2 = $"g.V('{session_id}').addE('belongs').to(g.V('{userId}'))";

As I wrote the program in C#, the first idea to be sure both of the above commands run or neither of them, is using TransactionScope in C#. However, it is not solved my problem as it is not on Cosmos DB. The question is how can I do these commands in a transaction on Graph Cosmos DB in C#?.

Is there any technique to resolve this in gremlin? Is graph.tx().commit() works on Cosmos DB?

// In A Transaction Scope
IDocumentQuery<dynamic> query = database.Client.CreateGremlinQuery<dynamic>(database.Graph, query1);
query.ExecuteNextAsync().Result;

query = database.Client.CreateGremlinQuery<dynamic>(database.Graph, query2);
query.ExecuteNextAsync().Result;
like image 427
OmG Avatar asked Nov 20 '25 00:11

OmG


1 Answers

Unfortunately, CosmosDB does not have support transactions when executing a gremlin command.

In your case, I would recommend using at least Session consistency level if you aren't already. This will guarantee you read your writes across multiple gremlin commands executed within the same session. By default, DocumentClient assumes the consistency level that the target Document collection is configured with.

Beyond this, the current support for gremlin commands in the SDK doesn't provide other techniques which would approximate transactions. There are other options available if choose to use Cosmos DB SQL commands or stored procs, but this would prevent you from using gremlin queries to operate on the data.

like image 96
Oliver Towers Avatar answered Nov 21 '25 14:11

Oliver Towers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!