Insert two node with go function has error:Transaction has been aborted. Please retry

package main

import (
“context”
“encoding/json”
“fmt”
“time”

"github.com/dgraph-io/dgraph/client"
"github.com/dgraph-io/dgraph/protos/api"
"google.golang.org/grpc"

)

type (
Person struct {
Name string json:"name,omitempty"
Friend Friend json:"friend,omitempty"
}
Friend struct {
Uid string json:"uid,omitempty"
}
)

var dg *client.Dgraph

func init() {
conn, err := grpc.Dial(“192.168.99.230:9080”, grpc.WithInsecure())
if err != nil {
fmt.Printf(“connect error %v”, err)
}
dg = client.NewDgraphClient(
api.NewDgraphClient(conn),
)
}
func MutateJSON(json byte) {
mu := &api.Mutation{
CommitNow: true,
}
mu.SetJson = json
txn := dg.NewTxn()
_, err := txn.Mutate(context.Background(), mu)
if err != nil {
fmt.Printf(“mutate error %v”, err)
}
}
func initBoss() {
p := Person{
Name: “boss”,
}
if json, err := json.Marshal§; err != nil {
fmt.Printf(“Marshal %v”, err)
} else {
fmt.Println(string(json))
MutateJSON(json)
}
}
func insertPerson(name string, to string) {
p := Person{
Name: name,
Friend: Friend{
Uid: to,
},
}
if json, err := json.Marshal§; err != nil {
fmt.Printf(“Marshal %v”, err)
} else {
fmt.Println(string(json))
MutateJSON(json)
}
}
func main() {
go insertPerson(“c”, “0x2818”)
go insertPerson(“d”, “0x2818”)
time.Sleep(5 * time.Second)
}

the 0x2818 is a exist person node.

the output is :

{“name”:“d”,“friend”:{“uid”:“0x2818”}}
{“name”:“c”,“friend”:{“uid”:“0x2818”}}
mutate error Transaction has been aborted. Please retry.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.