In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Node callback function makes database transactions do not seem to be as simple as java and php. Find some transaction packages on the Internet that are not as simple as you expected, so let's package one yourself.
The general idea of encapsulation is very simple: the function accepts a transaction function, and the logic of how to handle the transaction is determined by this function, rather than assembling multiple preprocessing SQL and parameters as the transaction encapsulated on the web. The transaction function requires that Promise be returned. In this way, we can use the Promise to determine whether to commit or roll back the transaction.
According to the above idea, the implementation code:
Let mysql = require ('mysql') let config = require ('.. / config') let pool = mysql.createPool (config.database) / / config.databa database configuration let trans = (tran) = > {return new Promise ((resolve, reject) = > {/ / returns the interface pool.getConnection ((err) that provides the success and failure of the transaction by promise Conn) = > {if (err) {reject (err)} else {conn.beginTransaction ((err) = > {/ / start transaction if (err) {conn.release () reject (err)) } else {let promise = tran (conn) / / call transaction function promise.then (result = > {conn.commit (err = > {/ / transaction function resolve) commit transaction if (err) { Reject (err)} else {resolve (result)})}) .catch (err = > { Conn.rollback (() = > {/ / transaction function reject rolls back the transaction conn.release () reject (err)}) })})}
The transaction function accepts the database connection object conn, where I encapsulate the database execution statement:
Trans.query = (conn, sql, params) = > {return new Promise ((resolve, reject) = > {conn.query (sql, params, (err, result) = > {if (err) {reject (err)} else {resolve (result)}
After the above transaction is encapsulated, the application is the same as the normal Promise:
Trans ((conn) = > {return trans.query (conn,db_ user.register.user, [username, password, new Date (). GetTime ()]) .then (result = > {return trans.query (conn,db_user.login) [username])}) .then (result = > {return trans.query (conn,db_user.register.profile, [result [0] .id Nickname])}) .then (result= > {console.log (transaction commit successful)}) .catch (err = > {console.log (transaction commit failed)})
The above case is a piece of code I intercepted from the project, from which it is obvious that the code can easily encapsulate its own logic and execute transaction processes with different paths.
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.