Use SQL transactions during sync
authorChristian Weiske <cweiske@cweiske.de>
Tue, 14 Oct 2014 05:54:56 +0000 (07:54 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Tue, 14 Oct 2014 16:12:45 +0000 (18:12 +0200)
controller/apicontroller.php

index 688d0b0a47d36920d78634329233e3590648420b..39de60d209de3080fa0d3341b601df29b8edfb07 100644 (file)
@@ -214,7 +214,7 @@ class ApiController extends Controller
             )
         );
         $syncdata = $this->notes->loadSyncData();
             )
         );
         $syncdata = $this->notes->loadSyncData();
-        
+
         $res = $this->handleNoteSave($username, $syncdata);
         if ($res instanceof \OCP\AppFramework\Http\Response) {
             return $res;
         $res = $this->handleNoteSave($username, $syncdata);
         if ($res instanceof \OCP\AppFramework\Http\Response) {
             return $res;
@@ -256,8 +256,8 @@ class ApiController extends Controller
             return;
         }
 
             return;
         }
 
-        //note that we have more data in $arPut than just our JSON
-        // request object merges it with other data
+        //Note that we have more data in $arPut than just our JSON.
+        // The request object merges it with other data.
         $arPut = $this->request->put;
 
         //structural validation
         $arPut = $this->request->put;
 
         //structural validation
@@ -285,23 +285,30 @@ class ApiController extends Controller
         }
 
         //update
         }
 
         //update
-        ++$syncdata->latestSyncRevision;
-        foreach ($arPut['note-changes'] as $noteUpdate) {
-            //owncloud converts object to array, so we reverse
-            $noteUpdate = (object) $noteUpdate;
-
-            $note = $this->notes->load($noteUpdate->guid);
-            if (isset($noteUpdate->command) && $noteUpdate->command == 'delete') {
-                $this->notes->delete($noteUpdate->guid);
-            } else {
-                $this->notes->update(
-                    $note, $noteUpdate, $syncdata->latestSyncRevision
-                );
-                $this->notes->save($note);
+        \OC_DB::beginTransaction();
+        try {
+            ++$syncdata->latestSyncRevision;
+            foreach ($arPut['note-changes'] as $noteUpdate) {
+                //owncloud converts object to array, so we reverse
+                $noteUpdate = (object) $noteUpdate;
+
+                $note = $this->notes->load($noteUpdate->guid);
+                if (isset($noteUpdate->command) && $noteUpdate->command == 'delete') {
+                    $this->notes->delete($noteUpdate->guid);
+                } else {
+                    $this->notes->update(
+                        $note, $noteUpdate, $syncdata->latestSyncRevision
+                    );
+                    $this->notes->save($note);
+                }
             }
             }
-        }
 
 
-        $this->notes->saveSyncData($syncdata);
+            $this->notes->saveSyncData($syncdata);
+            \OC_DB::commit();
+        } catch (\DatabaseException $e) {
+            \OC_DB::getConnection()->rollBack();
+            throw $e;
+        }
     }
 
     /**
     }
 
     /**