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?