Created
April 30, 2019 04:15
-
-
Save wkalt/08cfb70216a6420cae3eda83db209f02 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"database/sql" | |
"fmt" | |
_ "github.com/lib/pq" | |
) | |
func main() { | |
db, err := sql.Open("postgres", "") | |
if err != nil { | |
panic(err) | |
} | |
defer db.Close() | |
tx, err := db.Begin() | |
if err != nil { | |
panic(err) | |
} | |
// if you execute these in psql in a transaction, you get a rollback | |
query := `set local statement_timeout = '1s'; | |
select pg_sleep(10)` | |
_, err = tx.Exec(query) | |
if err != nil { | |
tx.Rollback() | |
fmt.Println("timed out") | |
} else { | |
tx.Commit() | |
fmt.Println("failed to time out") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment