The solution to the sqlite3 transaction after the commit transaction is not finished in Node-webkit

Source: Internet
Author: User
Tags sqlite database stmt

In Node-webkit, the local SQLite database is created with Sqlite3 libraries authoring, and the server data is synchronized locally, and if the volume of data is large, the transaction executes for a long time, and the callback returns quickly, not the expected execution of the transaction to return the result. A bug occurs when the real-time requirement is high or the subsequent processing relies on the completion of the transaction, and the callback is executed only if the transaction is completed with a circular query.

The example code is as follows, when the pubservices data has more than 20,000 processing time, it can be clearly seen that after the transaction has been processed after the callback but the data is not all saved in, the data will be a little bit of probability can be seen.

Var _=require ("underscore"); Var sqlite3=require ("Sqlite3");var basedb = new  Sqlite3. Database (yourpath+ '//database.db ');this.pubservicesync = function  (pubservices,callbacks) {         try{        // stml temporary variable of SQL         //readyinterval the variables of the stored loop query           //beginpubservicecount the number of variables stored in the query           var stmt,readyInterval,beginPubserviceCount=0;         Basedb.run ("Begin transaction"); Basedb.run ("Pragma synchronous=off");//Delete Service number          stmt = basedb.prepare ("Delete from pubservice");         stmt.run ();//Add service number Stmt = basedb.prepare ("insert into  pubservice  (Pubserviceid,namE,avatarid,recommend,createdat,updatedat)  values  ($pubserviceId, $name, $avatarId, $recommend, $ Createdat, $updatedAt) ") _.each (Pubservices,function (pubservice) {Stmt.run ({$pubserviceId: Pubservice.pubserviceid, $name:p ubservice.pubservicename, $avatarId:p Ubservice.avatarid, $recommend: Pubservice.recommend, $createdAt: New date (). toisostring (), $updatedAt: New date (). toisostring ()}); Stmt.finalize (function () {Basedb.run ("commit transaction", function (Err) {if (err) {callbacks (err);} Else{readyinterval = setinterval (function () {                         basedb.get ("Select  count (*)  as count from pubservice ", function (Err,result) {                                                          if (ERR) {                                  Clearinterval (Readyinterval);                                  readyInterval=null;                                 if (typeof callbacks ==  "function") callbacks (Err)                               }else{                                 var getUserCount=0;                                  if (typeof pubservices   !=  "undefined"  &&typeof pubservices.length !=  "undefined") {                                      getusercount= pubservices.length;                                 }                                  console.log ("Sync Service Number" +result.count+ "  Total:" + getusercount+ "  percent: " +parseint (result.count/getusercount*100) + "%")                                                                    if (pubservices&&pubservices.length) {                                             if (Result.count==beginpubservicecounT) {                                                 clearinterval (ReadyInterval);                                           readyInterval=null;                                          if (typeof callbacks ==  "function") Callbacks (NULL)                                      }else{                                       beginpubservicecount = result.count;                                      }                                  }else{                                                                            Clearinterval (Readyinterval);                                      readyInterval=null;                                      if (typeof callbacks ==  "function") callbacks ()                                  }                              }                          })                      },400);});}); catch (E) {        console.error ("Pubservicesync error:" +e);         }};}


The solution to the sqlite3 transaction after the commit transaction is not finished in Node-webkit

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.