func cleanUpCheck(sess sqlbuilder.Database) (err error) { var stats map[string]int stats, err = getStats(sess) if err != nil { return err } if activeStatements := sqladapter.NumActiveStatements(); activeStatements > 128 { return fmt.Errorf("Expecting active statements to be at most 128, got %d", activeStatements) } sess.ClearCache() if activeStatements := sqladapter.NumActiveStatements(); activeStatements != 0 { return fmt.Errorf("Expecting active statements to be 0, got %d", activeStatements) } for i := 0; i < 10; i++ { stats, err = getStats(sess) if err != nil { return err } if stats["Prepared_stmt_count"] != 0 { time.Sleep(time.Millisecond * 200) // Sometimes it takes a bit to clean prepared statements err = fmt.Errorf(`Expecting "Prepared_stmt_count" to be 0, got %d`, stats["Prepared_stmt_count"]) continue } break } return err }
// RunTx creates a transaction context and runs fn within it. func RunTx(d sqlbuilder.Database, fn func(tx sqlbuilder.Tx) error) error { tx, err := d.NewTx() if err != nil { return err } defer tx.Close() if err := fn(tx); err != nil { tx.Rollback() return err } return tx.Commit() }
func getStats(sess sqlbuilder.Database) (map[string]int, error) { stats := make(map[string]int) row := sess.Driver().(*sql.DB).QueryRow(`SELECT count(1) AS value FROM pg_prepared_statements`) var value int err := row.Scan(&value) if err != nil { return nil, err } stats["pg_prepared_statements_count"] = value return stats, nil }
func getStats(sess sqlbuilder.Database) (map[string]int, error) { stats := make(map[string]int) res, err := sess.Driver().(*sql.DB).Query(`SHOW GLOBAL STATUS LIKE '%stmt%'`) if err != nil { return nil, err } var result struct { VariableName string `db:"Variable_name"` Value int `db:"Value"` } iter := sqlbuilder.NewIterator(res) for iter.Next(&result) { stats[result.VariableName] = result.Value } return stats, nil }
func cleanUpCheck(sess sqlbuilder.Database) (err error) { var stats map[string]int stats, err = getStats(sess) if err != nil { return err } if activeStatements := sqladapter.NumActiveStatements(); activeStatements > 128 { return fmt.Errorf("Expecting active statements to be at most 128, got %d", activeStatements) } sess.ClearCache() stats, err = getStats(sess) if err != nil { return err } if stats["pg_prepared_statements_count"] != 0 { return fmt.Errorf(`Expecting "Prepared_stmt_count" to be 0, got %d`, stats["Prepared_stmt_count"]) } return nil }