wiki.techinc.nl/includes/deferred/AutoCommitUpdate.php
Aaron Schulz 3c21f0e8e5 Add AutoCommitUpdate class and replace some onTransactionIdle callers
* This puts the complex logic here after the commit step for
  all DBs, making the main multi-DB transaction more likely
  to be atomic.
* Made some cleanups to AtomicSectionUpdate and made it cancel
  if the transaction is rolled back as it should.
* Also cleaned up some closures for PHP 5.4.

Change-Id: If2f7bb6b1ba6daf1cfdc934f27c32b0b10431a3d
2016-07-21 05:24:28 +00:00

56 lines
1.2 KiB
PHP

<?php
/**
* Deferrable Update for closure/callback updates that should use auto-commit mode
* @since 1.28
*/
class AutoCommitUpdate implements DeferrableUpdate {
/** @var IDatabase */
private $dbw;
/** @var string */
private $fname;
/** @var callable */
private $callback;
/**
* @param IDatabase $dbw
* @param string $fname Caller name (usually __METHOD__)
* @param callable $callback Callback that takes (IDatabase, method name string)
*/
public function __construct( IDatabase $dbw, $fname, callable $callback ) {
$this->dbw = $dbw;
$this->fname = $fname;
$this->callback = $callback;
if ( $this->dbw->trxLevel() ) {
$this->dbw->onTransactionResolution( [ $this, 'cancelOnRollback' ] );
}
}
public function doUpdate() {
if ( !$this->callback ) {
return;
}
$autoTrx = $this->dbw->getFlag( DBO_TRX );
$this->dbw->clearFlag( DBO_TRX );
try {
/** @var Exception $e */
$e = null;
call_user_func_array( $this->callback, [ $this->dbw, $this->fname ] );
} catch ( Exception $e ) {
}
if ( $autoTrx ) {
$this->dbw->setFlag( DBO_TRX );
}
if ( $e ) {
throw $e;
}
}
public function cancelOnRollback( $trigger ) {
if ( $trigger === IDatabase::TRIGGER_ROLLBACK ) {
$this->callback = null;
}
}
}