Txmanager
No description available
Install / Use
/learn @shogo82148/TxmanagerREADME
txmanager
The txnmanager package is a nested transation manager for database/sql.
SYNOPSIS
Use TxBegin to start a transaction, and TxCommit or TxRollback to finish the transaction.
import (
"database/sql"
"github.com/shogo82148/txmanager"
)
func Example(db *sql.DB) {
dbm := txmanager.NewDB(db)
// start a transaction
tx, _ := dbm.TxBegin()
defer tx.TxFinish()
// Exec INSERT in a transaction
_, err := tx.Exec("INSERT INTO t1 (id) VALUES(1)")
if err != nil {
tx.TxRollback()
}
tx.TxCommit()
}
You can manage txmanager.DB with txmanager.Do.
import (
"database/sql"
"github.com/shogo82148/txmanager"
)
func Example(db *sql.DB) {
dbm := txmanager.NewDB(db)
txmanager.Do(dbm, func(tx txmanager.Tx) error {
// Exec INSERT in a transaction
_, err := tx.Exec("INSERT INTO t1 (id) VALUES(1)")
return err
})
}
NESTED TRANSACTION
import (
"database/sql"
"github.com/shogo82148/txmanager"
)
func Foo(dbm *txmanager.DB) error {
return txmanager.Do(dbm, func(tx txmanager.Tx) error {
_, err := tx.Exec("INSERT INTO t1 (id) VALUES(1)")
return err
})
}
func Example(db *sql.DB) {
dbm := txmanager.NewDB(db)
Foo(dbm)
txmanager.Do(dbm, func(tx txmanager.Tx) error {
return Foo(tx)
})
}
END HOOK
TxCommit necessarily does not do COMMIT SQL statemant.
So following code sometimes outputs wrong log.
func Foo(dbm *txmanager.DB) error {
err := txmanager.Do(dbm, func(tx txmanager.Tx) error {
_, err := tx.Exec("INSERT INTO t1 (id) VALUES(1)"); err != nil {
return err
})
if err != nil {
return err
}
// TxCommit is success, while the transaction might fail
log.Println("COMMIT is success!!!")
return nil
}
Use TxAddEndHook to avoid it.
It is inspired by DBIx::TransactionManager::EndHook.
func Foo(dbm *txmanager.DB) error {
return txmanager.Do(dbm, func(tx txmanager.Tx) error {
if _, err := tx.Exec("INSERT INTO t1 (id) VALUES(1)"); err != nil {
return err
}
tx.TxAddEndHook(func() error {
// It is called if all transactions are executed successfully.
log.Println("COMMIT is success!!!")
})
return nil
})
}
LICENSE
This software is released under the MIT License, see LICENSE.txt.
godoc
See godoc for more imformation.
Related Skills
node-connect
350.1kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
109.9kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
350.1kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
350.1kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
