您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)怎么在golang中實(shí)現(xiàn)mysql數(shù)據(jù)庫(kù)事務(wù)的提交與回滾,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
golang可以做服務(wù)器端開(kāi)發(fā),但golang很適合做日志處理、數(shù)據(jù)打包、虛擬機(jī)處理、數(shù)據(jù)庫(kù)代理等工作。在網(wǎng)絡(luò)編程方面,它還廣泛應(yīng)用于web應(yīng)用、API應(yīng)用等領(lǐng)域。
用到的庫(kù)有:
"github.com/jmoiron/sqlx" _ "github.com/go-sql-driver/mysql"
事務(wù)操作是通過(guò)三個(gè)方法實(shí)現(xiàn):
Begin():開(kāi)啟事務(wù)
Commit():提交事務(wù)(執(zhí)行sql)
Rollback():回滾
在事物里操作MySQL任意一步操作出錯(cuò),都需要Rollback()回滾。
package main import ( "fmt" "github.com/alecthomas/log4go" _ "github.com/go-sql-driver/mysql" "github.com/jmoiron/sqlx" ) var Db *sqlx.DB func init() { db,err:=sqlx.Open("mysql","TigerwolfC:123456@tcp(127.0.0.1:3306)/test?charset=utf8") if err != nil { fmt.Println("open mysql failed,", err) return } Db = db } func main() { mysqlTest() } func mysqlTest() error{ tx, err := Db.Begin() if err != nil { log4go.Error("open mysql database fail", err) return err } result, err := tx.Exec("INSERT INTO userinfo (username, password,department,email) VALUES (?, ?,?,?)","cici","33333","it","TigerwolfC@163.com") if err != nil{ fmt.Println("insert failed,error: ", err) tx.Rollback() return err } id,_ := result.LastInsertId() fmt.Println("insert id is :",id) _, err = tx.Exec("update userinfo set department = ? where username = ?","cekong","hahah") if err != nil{ fmt.Println("update failed error:",err) tx.Rollback() return err } else { fmt.Println("update success!") } _, err = tx.Exec("delete from userinfo where username = ? ", "weiwei") if err != nil{ fmt.Println("delete error:",err) tx.Rollback() return err }else{ fmt.Println("delete success") } return tx.Commit() }
當(dāng)然也可以用defer tx.Rollback(),在程序退出前回滾。
func mysqlTest() error{ tx, err := Db.Begin() if err != nil { log4go.Error("open mysql database fail", err) } defer tx.Rollback() result, err := tx.Exec("INSERT INTO userinfo (username, password,department,email) VALUES (?, ?,?,?)","cici","33333","it","TigerwolfC@163.com") if err != nil{ fmt.Println("insert failed,error: ", err) return err } id,_ := result.LastInsertId() fmt.Println("insert id is :",id) _, err = tx.Exec("update userinfo set department = ? where username = ?","cekong","hahah") if err != nil{ fmt.Println("update failed error:",err) return err } else { fmt.Println("update success!") } _, err = tx.Exec("delete from userinfo where username = ? ", "weiwei") if err != nil{ fmt.Println("delete error:",err) return err }else{ fmt.Println("delete success") } return tx.Commit() }
補(bǔ)充:數(shù)據(jù)庫(kù)事務(wù)處理(go,mysql)
//數(shù)據(jù)庫(kù)連接池 var db *sql.DB //初始化數(shù)據(jù)庫(kù) func initDB() (err error){ fmt.Println("學(xué)習(xí)數(shù)據(jù)庫(kù)") dsn := "usename:password@tcp(127.0.0.1:3306)/dataname" db, err = sql.Open("mysql",dsn) if err!=nil { fmt.Println("打開(kāi)數(shù)據(jù)庫(kù)失敗 err:",err,".dsn:",dsn) return } err = db.Ping() if err!=nil { fmt.Println("open failed err:",err) return } fmt.Println("連接數(shù)據(jù)庫(kù)成功") //最大連接數(shù) db.SetMaxOpenConns(10) //設(shè)置連接池中的最大閑置連接數(shù) db.SetMaxIdleConns(10) return } func trans() { tx, err := db.Begin() if err!=nil { fmt.Println("事務(wù)開(kāi)啟失敗, err:",err) return } sqlStr1 := "update student set age=age-2 where id=1;" sqlStr2 := "update student set age=age+2 where id=3;" _, err = tx.Exec(sqlStr1) if err!=nil { fmt.Println("修改失敗, err",err,",sqlStr1:",sqlStr1) tx.Rollback() return } _, err = tx.Exec(sqlStr2) if err!=nil { fmt.Println("修改失敗, err",err,",sqlStr2:",sqlStr2) tx.Rollback() return } fmt.Println("執(zhí)行成功") //提交事務(wù) tx.Commit() } func main() { fmt.Println("mysql 002.事務(wù)處理") initDB() trans() }
上述就是小編為大家分享的怎么在golang中實(shí)現(xiàn)mysql數(shù)據(jù)庫(kù)事務(wù)的提交與回滾了,如果剛好有類(lèi)似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。