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