[FileRepo] Changed LocalFile locking to avoid breaking transactions.
authorAaron Schulz <aschulz@wikimedia.org>
Wed, 20 Feb 2013 19:28:27 +0000 (11:28 -0800)
committerAaron Schulz <aschulz@wikimedia.org>
Wed, 20 Feb 2013 19:30:38 +0000 (11:30 -0800)
* Only BEGIN on lock() if no trx was in progress.
  Likewise, only COMMIT in unlock() if the lock() call started a trx.
* This can avoid problems with commiting page update transactions
  prematurely, which could leave broken page stub rows. (bug 40178)

Change-Id: I9a0adb25ee107df9a6bf70c6103ddfb7f034be25

includes/filerepo/file/LocalFile.php

index ee49448..27386b4 100644 (file)
@@ -71,6 +71,7 @@ class LocalFile extends File {
                $extraDataLoaded,  # Whether or not lazy-loaded data has been loaded from the database
                $upgraded,         # Whether the row was upgraded on load
                $locked,           # True if the image row is locked
+               $lockedOwnTrx,     # True if the image row is locked with a lock initiated transaction
                $missing,          # True if file is not present in file system. Not to be cached in memcached
                $deleted;          # Bitfield akin to rev_deleted
 
@@ -1669,7 +1670,10 @@ class LocalFile extends File {
                $dbw = $this->repo->getMasterDB();
 
                if ( !$this->locked ) {
-                       $dbw->begin( __METHOD__ );
+                       if ( !$dbw->trxLevel() ) {
+                               $dbw->begin( __METHOD__ );
+                               $this->lockedOwnTrx = true;
+                       }
                        $this->locked++;
                }
 
@@ -1684,9 +1688,10 @@ class LocalFile extends File {
        function unlock() {
                if ( $this->locked ) {
                        --$this->locked;
-                       if ( !$this->locked ) {
+                       if ( !$this->locked && $this->lockedOwnTrx ) {
                                $dbw = $this->repo->getMasterDB();
                                $dbw->commit( __METHOD__ );
+                               $this->lockedOwnTrx = false;
                        }
                }
        }
@@ -1698,6 +1703,7 @@ class LocalFile extends File {
                $this->locked = false;
                $dbw = $this->repo->getMasterDB();
                $dbw->rollback( __METHOD__ );
+               $this->lockedOwnTrx = false;
        }
 
        /**