avito-tech/go-transaction-manager
avito-tech/go-transaction-manager has 426 GitHub stars and 24 forks. GitHot measured 0 stars in 24 hours and +4 in 7 days. Last checked 2026-11-12 18:19 UTC. Last push 2026-09-26.
Transaction manager for GoLang
측정된 스타 증가
- in 24h
- 0
- in 7 days
- +4
- in 15 days
- 기록 수집 중
- in 1 month
- 기록 수집 중
서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.
README.md
OriginalGo 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)
[!WARNING] The pgx v4 and pgx v5 transactions are not safe for concurrent use:
pgx.Txdoes not support running commands from multiple goroutines simultaneously (jackc/pgx#2332). Never run a query concurrently withCommit/Rollbackon the same transaction. Context cancellation does not roll back the transaction from a background goroutine — with the manager the rollback runs after your function returns; standalone callers must callRollbackexplicitly.
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.
The critical bugs are firstly solved for the most recent two Golang versions and then for older ones if it is simple.
[!CAUTION] Disclaimer: Keep your dependencies up to date, even indirect ones.
go get -u && go mod tidyhelps you.Note: The go-transaction-manager uses some old dependencies to support backwards compatibility for old versions of Go.
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.
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
Requirements
Local Running
- To install all dependencies use
make go.mod.tidyormake go.mod.vendor. - To run all tests use
make testormake test.with_real_dbfor integration tests.
The root go.work workspace already wires every driver module to the local trm
checkout, so go build/go test run from a driver directory (e.g. drivers/sql) resolve
trm/v2 locally without any change to that driver's go.mod. Do not add a replace directive
for trm/v2 to a driver's go.mod — it is unnecessary inside the workspace and breaks consumers
who go get the module outside of it. The commented-out go mod edit -replace=... lines in each
driver's go.mod are only a recipe for building that module standalone, outside the workspace.
To run database by docker, there is docker-compose.yaml.
docker compose -f trm/drivers/test/docker-compose.yaml up
For full GitHub Actions run, you can use act.
Running old go versions
To stop Golang upgrading set environment variable GOTOOLCHAIN=local .
go install go1.16 # or older version
go1.16 install
Use -mod=readonly to prevent go.mod modification.
To run tests
go1.16 test -race -mod=readonly ./...
How to bump up Golang version in CI/CD
- Changes in .github/workflows/main.yaml.
- Add all old version of Go in
go-version:fortests-unitsjob. - Update
go-version:on current version of Go forlintandtests-integrationjobs.
- Add all old version of Go in
- Update build tags by replacing
build go1.xxon new version.
Resolve problems with old version of dependencies
To build go.mod compatible for old version use go mod tidy -compat=1.13 (docs).
However, --compat doesn't always work correct and we need to set some library versions manually.
go get go.uber.org/[email protected]in trm, sql, sqlx.go get github.com/mattn/[email protected]in trm, sql, sqlx.go get github.com/stretchr/[email protected]in trm, sql, sqlx, goredis8, mongo.go get github.com/jackc/[email protected]in pgxv4. Golang version was bumped up from 1.12 to 1.17 in pgconn v1.14.3.go get golang.org/x/[email protected]in pgxv4.
Also, you can change version of single dependency.
go mod edit -require=github.com/avito-tech/go-transaction-manager/trm/[email protected]
go mod tidy
Authors
- Ilia Sergunin github/maranqz
관련 프로젝트 살펴보기
Asabeneh/30-Days-Of-Python
30 Days of Python 프로그래밍 챌린지는 Python 프로그래밍 언어를 30일 동안 배우기 위한 단계별 가이드입니다. 이 챌린지는 100일 이상 걸릴 수 있습니다. 자신의 속도에 맞춰 진행하세요. 다음 동영상도 도움이 될 수 있습니다: https://www.youtube.com/channel/UC7PNRuno1rzYPb1xLa4yktw
78,618 스타grafana/grafana
개방적이고 조합 가능한 옵저버빌리티 및 데이터 시각화 플랫폼입니다. Prometheus, Loki, Elasticsearch, InfluxDB, Postgres 등 여러 소스의 메트릭, 로그, 트레이스를 시각화합니다.
77,859 스타redis/redis
실시간 데이터 기반 애플리케이션을 구축하는 개발자에게 Redis는 가장 선호되고, 가장 빠르며, 기능이 가장 풍부한 캐시, 데이터 구조 서버이자 문서 및 벡터 쿼리 엔진입니다.
77,472 스타apache/superset
Apache Superset는 데이터 시각화 및 데이터 탐색 플랫폼입니다.
75,495 스타