go-transaction-manager
go-transaction-manager copied to clipboard
Transaction manager for GoLang
Go transaction manager
Transaction manager is an abstraction to coordinate database transaction boundaries.
Easiest way to get the perfect repository.
Supported implementations
- database/sql, docs ( Go 1.13)
- jmoiron/sqlx, docs ( Go 1.13)
- gorm, docs ( Go 1.18)
- mongo-go-driver, docs ( Go 1.13)
- go-redis/redis, docs ( Go 1.17)
- pgx_v4, docs ( Go 1.16)
- pgx_v5, docs ( Go 1.19)
Installation
go get github.com/avito-tech/go-transaction-manager/trm/v2
To install some support database use go get github.com/avito-tech/go-transaction-manager/drivers/{name}
.
For example go get github.com/avito-tech/go-transaction-manager/drivers/sqlx/v2
.
Backwards Compatibility
The library is compatible with the most recent two versions of Go. Compatibility beyond that is not guaranteed.
Usage
To use multiple transactions from different databases, you need to set CtxKey in Settings by WithCtxKey (docs).
For nested transactions with different transaction managers, you need to use ChainedMW (docs).
To skip a transaction rollback due to an error, use ErrSkip or Skippable
Explanation of the approach English, Russian article and youtube.
Examples with an ideal repository and nested transactions.
- database/sql
- jmoiron/sqlx
- gorm
- mongo-go-driver
- go-redis/redis
- pgx_v4
- pgx_v5
Below is an example how to start usage.
package main
import (
"context"
"fmt"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
trmsqlx "github.com/avito-tech/go-transaction-manager/drivers/sqlx/v2"
"github.com/avito-tech/go-transaction-manager/trm/v2/manager"
)
func main() {
db, err := sqlx.Open("sqlite3", "file:test?mode=memory")
checkErr(err)
defer db.Close()
sqlStmt := `CREATE TABLE IF NOT EXISTS user (user_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, username TEXT);`
_, err = db.Exec(sqlStmt)
checkErr(err, sqlStmt)
r := newRepo(db, trmsqlx.DefaultCtxGetter)
ctx := context.Background()
trManager := manager.Must(trmsqlx.NewDefaultFactory(db))
u := &user{Username: "username"}
err = trManager.Do(ctx, func(ctx context.Context) error {
checkErr(r.Save(ctx, u))
// example of nested transactions
return trManager.Do(ctx, func(ctx context.Context) error {
u.Username = "new_username"
return r.Save(ctx, u)
})
})
checkErr(err)
userFromDB, err := r.GetByID(ctx, u.ID)
checkErr(err)
fmt.Println(userFromDB)
}
func checkErr(err error, args ...interface{}) {
if err != nil {
panic(fmt.Sprint(append([]interface{}{err}, args...)...))
}
}
type repo struct {
db *sqlx.DB
getter *trmsqlx.CtxGetter
}
func newRepo(db *sqlx.DB, c *trmsqlx.CtxGetter) *repo {
return &repo{db: db, getter: c}
}
type user struct {
ID int64 `db:"user_id"`
Username string `db:"username"`
}
func (r *repo) GetByID(ctx context.Context, id int64) (*user, error) {
query := "SELECT * FROM user WHERE user_id = ?;"
u := user{}
return &u, r.getter.DefaultTrOrDB(ctx, r.db).GetContext(ctx, &u, r.db.Rebind(query), id)
}
func (r *repo) Save(ctx context.Context, u *user) error {
query := `UPDATE user SET username = :username WHERE user_id = :user_id;`
if u.ID == 0 {
query = `INSERT INTO user (username) VALUES (:username);`
}
res, err := sqlx.NamedExecContext(ctx, r.getter.DefaultTrOrDB(ctx, r.db), r.db.Rebind(query), u)
if err != nil {
return err
} else if u.ID != 0 {
return nil
} else if u.ID, err = res.LastInsertId(); err != nil {
return err
}
return err
}
Benchmark
Comparing examples with and without trm.
Contribution
- To local development sync dependencies use
make go.work.sync
. - After finalizing of changes bump up version in all drivers.
- To install all dependencies use
make go.mod.tidy
ormake go.mod.vendor
. - To run all tests use
make go.test
ormake go.test.with_real_db
for integration tests.