Transactions using gremlin via golang

70 views
Skip to first unread message

shrinkhala singhania

unread,
Mar 6, 2025, 4:27:41 PMMar 6
to Gremlin-users
Hi all,

I am new to gremlin and am tinkering with transactions on a Gremlin server. I am essentially trying to do the following:

- add a few vertices
- start a transaction, drop the vertices, re-add them, commit

The commit however is failing with this error:

{code:244 message:Conflict: element modified in another transaction attributes:map[exceptions:[org.apache.tinkerpop.gremlin.structure.util.TransactionException]

This is my code:

package main

import (
"fmt"
"log"
"time"

gremlingo "github.com/apache/tinkerpop/gremlin-go/v3/driver"
)

func main() {
client, err := gremlingo.NewDriverRemoteConnection("ws://localhost:8182/gremlin")
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
defer client.Close()

g := gremlingo.Traversal_().WithRemote(client)

// Drop existing vertices
errChan := g.V().HasLabel("person").Drop().Iterate()
for err := range errChan {
if err != nil {
panic(fmt.Errorf("dropping all vertices: %w", err))
}
}

fmt.Println("Vertices dropped")

g2 := gremlingo.Traversal_().WithRemote(client) // New traversal instance

// Add 2 vertices initially
err = addVertices(g2)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Vertices added")

// Begin transaction
transaction := g3.Tx()
traversal, err := transaction.Begin()
if err != nil {
fmt.Println(err)
return
}

fmt.Println("Begin")
// Drop all persons inside the transaction
errChan = traversal.V().Drop().Iterate()
for err := range errChan {
if err != nil {
fmt.Println("error hit")
err2 := transaction.Rollback()
if err2 != nil {
fmt.Println(err2)
return
}
return
}
}

query := g.V().HasLabel("person").Id()
result, err := query.ToList()
if err != nil {
log.Printf("Error retrieving vertices: %v", err)
}
fmt.Println("Display all vertices, should be 0")
for _, res := range result {
fmt.Printf("%v\n", res.Data.(string))
}

// Add vertices within the transaction
err = addVertices(traversal)
if err != nil {
fmt.Println(err)
err2 := transaction.Rollback()
if err2 != nil {
fmt.Println(err2)
return
}
return
}

fmt.Println("vertices added within a transaction")

// Commit the transaction
errCommit := transaction.Commit()
if errCommit != nil {
fmt.Println("committing transaction was unsuccessful")
}
}

func addVertices(g *gremlingo.GraphTraversalSource) error {
traversal := g.GetGraphTraversal().AddV("person").Property(gremlingo.T.Id, "Foo").Fold().AddV("person").Property(gremlingo.T.Id, "Bar").Fold()
fmt.Println(Translate(traversal))
errChan := traversal.Iterate()
for err := range errChan {
if err != nil {
return fmt.Errorf("error adding initial vertices: %w", err)
}
}
return nil
}

func Translate(traversal *gremlingo.GraphTraversal) string {
if traversal == nil || traversal.Bytecode == nil {
return ""
}
translator := gremlingo.NewTranslator("g")

queryTranslation, err := translator.Translate(traversal.Bytecode)
if err != nil {
panic(err)
}

return queryTranslation
} 

What am I doing wrong?

Cole Greer

unread,
Mar 7, 2025, 7:15:05 PMMar 7
to Gremlin-users

Hi,

It appears that this is a bug in the current transaction implementation in TinkerGraph. A related bug fix has recently been merged into TinkerPop but not yet released, however there appears to be a bit more going on here than what is included in that fix.

This will need more investigation to fully explain what's going on, but I can confirm now that this is an bug with TinkerGraph and not anything wrong with your usage.

Cole Greer

unread,
Mar 12, 2025, 5:03:51 PMMar 12
to Gremlin-users
As an update, 2 new JIRAs have been opened regarding deletes in TinkerGraph with transactions, related to the issue described here:

https://issues.apache.org/jira/browse/TINKERPOP-3141
https://issues.apache.org/jira/browse/TINKERPOP-3142
Reply all
Reply to author
Forward
0 new messages