https://github.com/lestrrat-go/tx-guard
Simple database transaction guard
https://github.com/lestrrat-go/tx-guard
Last synced: 4 months ago
JSON representation
Simple database transaction guard
- Host: GitHub
- URL: https://github.com/lestrrat-go/tx-guard
- Owner: lestrrat-go
- License: mit
- Created: 2015-07-09T04:53:51.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2018-02-20T22:15:57.000Z (over 7 years ago)
- Last Synced: 2025-01-09T03:41:42.591Z (6 months ago)
- Language: Go
- Size: 6.84 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# tx-guard
Simple database transaction guard
[](https://travis-ci.org/lestrrat-go/tx-guard)
[](https://godoc.org/github.com/lestrrat-go/tx-guard)
# SYNOPSIS
```go
import (
"database/sql""github.com/lestrrat-go/tx-guard"
)func main() {
db, err := guard.Open("mysql", "....")
if err != nil {
println(err.Error())
return
}tx, err := db.Begin()
if err != nil {
println(err.Error())
return
}
// if tx.Commit or tx.Rollback is never explicitly
// called, the transaction is automatically rolled back
defer tx.AutoRollback()// do stuff with tx, maybe insert and then commit
}
```# DESCRIPTION
Often times we would like to have an automatic rollback in case we return
early from a method performing an SQL transaction operation. This small
wrapper creates transaction objects that have an `AutoRollback` method that
you can safely register to a `defer` statement so that in case of errors
`Rollback` gets called.Note that as of Go 1.7 which introduced the use of context.Context, this sort
of hack is no longer required.