Begin transactions explicitely in Job class.
authorDaniel Kinzler <daniel.kinzler@wikimedia.de>
Wed, 26 Sep 2012 10:29:44 +0000 (10:29 +0000)
committerdaniel <daniel.kinzler@wikimedia.de>
Wed, 26 Sep 2012 11:19:00 +0000 (13:19 +0200)
This reverts commit dfbf524d3561e3fc54a9a072fda173435acc2e14 which
was "Removed unmatched commits in Job class".

The original change was reverted n the light of the recent discussion about the DBO_TRX flag
and the intended use and bahavior of begin and commit.

See http://www.gossamer-threads.com/lists/wiki/wikitech/300087 for Tim's explanation of DBO_TRX.

This change now implements a new, alternative solution to the original problem of unmatched
calls to commit:

The Job class originally relied on implicitely started transactions. Introducing
explicit calls to begin() avoids warnings while running tests, and
causes transaxctiosn to be used also in cli (maintenance) mode.

Change-Id: I6ecb8faad06449331a79b81860fe64624d3694d4

includes/job/Job.php

index 45f0570..b47acc4 100644 (file)
@@ -151,16 +151,21 @@ abstract class Job {
 
                // Try to delete it from the master
                $dbw = wfGetDB( DB_MASTER );
+               $dbw->begin( __METHOD__ );
                $dbw->delete( 'job', array( 'job_id' => $row->job_id ), __METHOD__ );
                $affected = $dbw->affectedRows();
+               $dbw->commit( __METHOD__ );
 
                if ( !$affected ) {
+                       $dbw->begin( __METHOD__ );
+
                        // Failed, someone else beat us to it
                        // Try getting a random row
                        $row = $dbw->selectRow( 'job', array( 'minjob' => 'MIN(job_id)',
                                'maxjob' => 'MAX(job_id)' ), '1=1', __METHOD__ );
                        if ( $row === false || is_null( $row->minjob ) || is_null( $row->maxjob ) ) {
                                // No jobs to get
+                               $dbw->rollback( __METHOD__ );
                                wfProfileOut( __METHOD__ );
                                return false;
                        }
@@ -170,12 +175,14 @@ abstract class Job {
                        if ( $row === false ) {
                                // Random job gone before we got the chance to select it
                                // Give up
+                               $dbw->rollback( __METHOD__ );
                                wfProfileOut( __METHOD__ );
                                return false;
                        }
                        // Delete the random row
                        $dbw->delete( 'job', array( 'job_id' => $row->job_id ), __METHOD__ );
                        $affected = $dbw->affectedRows();
+                       $dbw->commit( __METHOD__ );
 
                        if ( !$affected ) {
                                // Random job gone before we exclusively deleted it