Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bk-iam 事务回滚方法建议优化 #260

Open
Han-Ya-Jun opened this issue Aug 1, 2023 · 0 comments
Open

bk-iam 事务回滚方法建议优化 #260

Han-Ya-Jun opened this issue Aug 1, 2023 · 0 comments

Comments

@Han-Ya-Jun
Copy link
Member

代码:https://github.com/TencentBlueKing/bk-iam/blob/master/pkg/database/utils.go
使用场景:

        // 生成统一的DB事务
	tx, err := database.GenerateDefaultDBTx()
	defer database.RollBackWithLog(tx)
        // err如果不为nil,tx会为nil
	if err != nil {
		err = errorWrapf(err, "define tx fail")
		return
	}
// RollBackWithLog will rollback and log if error
func RollBackWithLog(tx *sqlx.Tx) {
        // 这里加个tx nil判断 return最好
	err := tx.Rollback()
	if err != sql.ErrTxDone && err != nil {
		logging.GetSQLLogger().Error(err)
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant