3 use Wikimedia\Rdbms\IDatabase;
6 * Deferrable Update for closure/callback updates that should use auto-commit mode
9 class AutoCommitUpdate implements DeferrableUpdate, DeferrableCallback {
14 /** @var callable|null */
18 * @param IDatabase $dbw
19 * @param string $fname Caller name (usually __METHOD__)
20 * @param callable $callback Callback that takes (IDatabase, method name string)
22 public function __construct( IDatabase $dbw, $fname, callable $callback ) {
24 $this->fname = $fname;
25 $this->callback = $callback;
27 if ( $this->dbw->trxLevel() ) {
28 $this->dbw->onTransactionResolution( [ $this, 'cancelOnRollback' ], $fname );
32 public function doUpdate() {
33 if ( !$this->callback ) {
37 $autoTrx = $this->dbw->getFlag( DBO_TRX );
38 $this->dbw->clearFlag( DBO_TRX );
40 /** @var Exception $e */
42 call_user_func_array( $this->callback, [ $this->dbw, $this->fname ] );
43 } catch ( Exception $e ) {
46 $this->dbw->setFlag( DBO_TRX );
53 public function cancelOnRollback( $trigger ) {
54 if ( $trigger === IDatabase::TRIGGER_ROLLBACK ) {
55 $this->callback = null;
59 public function getOrigin() {