Browse Source

Merge pull request #346 from statonlab/add-db-transaction

Add db transaction to tripal_update_cvtermpath
Bradford Condon 6 years ago
parent
commit
19c7f5b06d
1 changed files with 11 additions and 4 deletions
  1. 11 4
      tripal_chado/api/modules/tripal_chado.cv.api.inc

+ 11 - 4
tripal_chado/api/modules/tripal_chado.cv.api.inc

@@ -386,6 +386,12 @@ function chado_update_cvtermpath($cv_id, $job_id = NULL){
   // TODO: there's a function to determine the current Chado instance.
   // we should use that.
   $prev_db = chado_set_active('chado');
+
+  print "\nNOTE: Updating CV Term Path is performed using a database transaction. \n" .
+    "If the update fails or is terminated prematurely then any new changes \n" .
+    "made will be rolled back.\n\n";
+  $transaction = db_transaction();
+
   try {
     $result = db_query('
       SELECT DISTINCT t.*
@@ -405,12 +411,13 @@ function chado_update_cvtermpath($cv_id, $job_id = NULL){
     }
   }
   catch (Exception $e) {
-    // If there's an exception we have to set the database back. So, do that
-    // and then rethrow the error.
-    chado_set_active($prev_db);
+    // Rollback any database changes
+    $transaction->rollback();
     throw $e;
+  } finally {
+    // Set the database back
+    chado_set_active($prev_db);
   }
-  chado_set_active($prev_db);
 }
 
 /**