//将CloseJira状态更新到数据库中
问题陈述-如果回滚触发,Golang如何以及在何处调用autogenicjiraclose()函数来运行某些操作。
func CloseJira(qMonName string) {
tx, err := dbCon.Begin()
notifier.CheckErr(err, "CloseJira() -> tx -> dbCon.Begin()", dbErrLog)
defer tx.Rollback()
stmt, errDBPrepare := tx.Prepare("update TABLE1 set
Key=NULL, StatusKey='Closed', Statustime_UTC=? where Name=?")
//Update db table record
notifier.CheckErr(errDBPrepare, "updateCloseJira() -> dbCon.Prepare()",
dbErrLog)
defer stmt.Close() // danger!
res, errStmtExec := stmt.Exec(time.Now().UTC(), qMonName)
notifier.CheckErr(errStmtExec, "CloseJira() -> stmt.Exec()", dbErrLog)
err = tx.Commit()
notifier.CheckErr(err, "CloseJira() -> err -> tx.Commit()", dbErrLog)
_, errRowAffected := res.RowsAffected()
notifier.CheckErr(errRowAffected, "CloseJira() -> res.RowsAffected()",
dbErrLog)
}
这是我需要交易时经常使用的模式:
func Foo() (err error) {
var tx *sql.Tx
tx, err = db.Begin()
if err != nil {
return err
}
defer func() {
if err == nil {
tx.Commit()
} else {
tx.Rollback()
}
}()
// Do whatever you want here.
}
诀窍在于,延迟功能将始终在最后运行。如果函数返回错误,则可以获取它并回滚(并执行其他所需的其他操作),如果函数正常返回,则可以提交事务。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句