func UpdateFeedWithFetchSuccess(db *pgx.ConnPool, feedID int32, update *ParsedFeed, etag String, fetchTime time.Time) error { _, err := db.Prepare("updateFeedWithFetchSuccess", updateFeedWithFetchSuccessSQL) if err != nil { return err } tx, err := db.Begin() if err != nil { return err } defer tx.Rollback() _, err = tx.Exec("updateFeedWithFetchSuccess", update.Name, fetchTime, &etag, feedID) if err != nil { return err } if len(update.Items) > 0 { insertSQL, insertArgs := buildNewItemsSQL(feedID, update.Items) _, err = tx.Exec(insertSQL, insertArgs...) if err != nil { return err } } return tx.Commit() }
func txInsertRollback(pool *pgx.ConnPool, actionNum int) error { tx, err := pool.Begin() if err != nil { return err } sql := ` insert into widgets(name, description, creation_time) values($1, $2, $3)` _, err = tx.Exec(sql, fake.ProductName(), fake.Sentences(), time.Now()) if err != nil { return err } return tx.Rollback() }
func txMultipleQueries(pool *pgx.ConnPool, actionNum int) error { tx, err := pool.Begin() if err != nil { return err } defer tx.Rollback() errExpectedTxDeath := errors.New("Expected tx death") actions := []struct { name string fn func() error }{ {"insertUnprepared", func() error { return insertUnprepared(tx, actionNum) }}, {"queryRowWithoutParams", func() error { return queryRowWithoutParams(tx, actionNum) }}, {"query", func() error { return query(tx, actionNum) }}, {"queryCloseEarly", func() error { return queryCloseEarly(tx, actionNum) }}, {"queryErrorWhileReturningRows", func() error { err := queryErrorWhileReturningRows(tx, actionNum) if err != nil { return err } return errExpectedTxDeath }}, } for i := 0; i < 20; i++ { action := actions[rand.Intn(len(actions))] err := action.fn() if err == errExpectedTxDeath { return nil } else if err != nil { return err } } return tx.Commit() }