In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to achieve the unified encapsulation of db transactions in golang, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Example of a process for transaction processing
Database: = db.DB tx, err: = database.Begin () if err! = nil {return err} stmt, err: = tx.Prepare (sqlQuery) if err! = nil {tx.Rollback () return err} _, err = stmt.Exec (paras...) If err! = nil {tx.Rollback () return err} err = tx.Commit () if err! = nil {tx.Rollback () return err}
The above are the general operations when we use transactions. It would be troublesome to rewrite the code every time we do a transaction, especially when something goes wrong, Rollback needs to be called many times in different error places.
Simple package
The first step in laziness
Using defer to deal with Rollback
Defer tx.Rollback ()
Rollback is performed regardless of whether it is successful or not, but it has some impact. If you still call Rollback successfully, an error will be reported. Although it can be ignored, as a programmer, it is necessary to make further adjustments.
The second step of laziness
Choose to execute Rollback according to the execution result to avoid invalid use.
Defer func () {/ / choose to execute Rollback if err! = nil & & tx! = nil {log.Println ("ExecSqlWithTransaction defer err:", err) tx.Rollback ()} ()
In this way, we can decide whether to Rollback or not based on the execution result of the transaction.
The third step of laziness
Encapsulation, the above code itself has great universality, so we extract the general parameters and encapsulate the process into a func, which can be called directly later.
Func ExecSqlWithTransaction (database * sql.DB, query string, args... interface {}) (err error) {tx, err: = database.Begin () if err! = nil {return err} defer func () {if err! = nil & & tx! = nil {tx.Rollback ()} () stmt Err: = tx.Prepare (query) if err! = nil {return err} defer stmt.Close () _, err = stmt.Exec (args...) If err! = nil {return err} return tx.Commit ()}
After encapsulation, we can use it as follows:
If err: = ExecSqlWithTransaction (database,sqlQuery,paras...); err! = nil {/ / error handling}
Is it very concise after encapsulation?
Further encapsulation
Multiple SELECT, UPDATE and other operations may occur in a transaction. The above encapsulation only deals with one operation, which can not meet our actual needs, so further encapsulation is needed.
Func ExecSqlWithTransaction (db * sql.DB, handle func (tx * sql.Tx) error) (err error) {tx, err: = db.Begin () if err! = nil {return err} defer func () {if err! = nil {tx.Rollback ()} () if err = handle (tx); err! = nil {return err} return tx.Commit ()}
You can directly use transaction tx to add, delete, modify and query in handle func.
These are all the contents of the article "how to achieve unified encapsulation of db transactions in golang". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.