go-cq / cq

neo4j cypher library for database/sql in go
MIT License
133 stars 11 forks source link

Error using transaction as shown in example code #30

Closed jetbasrawi closed 3 years ago

jetbasrawi commented 8 years ago

Hi,

When I try to use the transactional code as shown on the readme, it fails with the following error.

2016/05/29 09:45:42 parsing time "" as "Mon, 02 Jan 2006 15:04:05 -0700": cannot parse "" as "Mon" &{ 0xc820065e00 http http://***:****@localhost:7474/db/data/cypher http://***:******@localhost:7474/db/data/transaction <nil>}
09:45:42.422369 FTL TopicGraphReadModel Error committing transation
   err: commit errors: {[{"Neo.ClientError.Transaction.TransactionNotFound" "Unrecognized transaction id. Transaction may have timed out and been rolled back."}]}

The code that I am using to execute this is below. It is virtually identical to your code but builds up the statements with a range over a slice. I also have my db at global scope where in your code you open a db in the local method scope. Other than this it is the same.

addNameCreate = `MERGE (t:Topic {aggregateID:{0}})
                     CREATE (n:TopicName { Name : {1}, Culture: {2}, Domain: {3} })
                     CREATE (n)-[r:IS_NAME_FOR {Count:{4}}]->(t)`
tx, err := db.Begin()
    if err != nil {
        logger.Fatal("Error beginning transaction", "err", err)
    }

    stmt, err := tx.Prepare(addNameCreate)
    if err != nil {
        logger.Fatal("Error preparing transation", "err", err)
    }

    for _, event := range evs {
        if ev, ok := event.Event().(*TopicNameAdded); ok {
            stmt.Exec(event.AggregateID(), ev.Name, ev.Culture, ev.Domain, ev.SourceCount)
            continue
        }
        logger.Fatal("Unable to cast event to TopicNameAdded")
    }

    err = tx.Commit()
    if err != nil {
        logger.Fatal("Error committing transation", "err", err)
    }

Any ideas

yanpozka commented 7 years ago

I'm new using this library, but taking a first look at your code, you're ignoring possible errors when calling stmt.Exec(...) and because that you're not rolling back tx.Rollback() we should do a Rollback when there's errors in a transaction context, and that's why there's this sentence in your logs: ... Transaction may have timed out and been rolled back