I understand how to use squirrel and transactions separately, but I don't understand how to use them together. When should I rollback or commit? Is my attempt correct or not? If not, where am I wrong...
tx, err := db.repo.GetDatabase().Begin()
if err != nil {
return nil, err
}
sb := squirrel.StatementBuilder.
Insert("dependencies").
Columns("correlation_id", "name", "age").
PlaceholderFormat(squirrel.Dollar).
RunWith(db.repo.GetDatabase())
for _, human:= range humans{
sb = sb.Values(
human.CorrelationID,
human.Name,
human.Age,
)
}
_, err = sb.Exec()
if err != nil {
if err := tx.Rollback(); err != nil {
return nil, err
}
}
if err := tx.Commit(); err != nil {
return nil, err
}
As I understand it, I'm trying to rollback or commit after the query is executed in postgresql