3 namespace MediaWiki\Deferred
;
5 use Wikimedia\Rdbms\IDatabase
;
8 * Deferrable Update for closure/callback updates that should use auto-commit mode
11 class AutoCommitUpdate
implements DeferrableUpdate
, DeferrableCallback
{
16 /** @var callable|null */
20 * @param IDatabase $dbw DB handle; update aborts if a transaction now this rolls back
21 * @param string $fname Caller name (usually __METHOD__)
22 * @param callable $callback Callback that takes (IDatabase, method name string)
23 * @param IDatabase[] $conns Cancel the update if a transaction on these
24 * connections is rolled back [optional]
26 public function __construct( IDatabase
$dbw, $fname, callable
$callback, array $conns = [] ) {
28 $this->fname
= $fname;
29 $this->callback
= $callback;
30 // Register DB connections for which uncommitted changes are related to this update
32 foreach ( $conns as $conn ) {
33 if ( $conn->trxLevel() ) {
34 $conn->onTransactionResolution( [ $this, 'cancelOnRollback' ], $fname );
39 public function doUpdate() {
40 if ( !$this->callback
) {
44 $autoTrx = $this->dbw
->getFlag( DBO_TRX
);
45 $this->dbw
->clearFlag( DBO_TRX
);
47 ( $this->callback
)( $this->dbw
, $this->fname
);
50 $this->dbw
->setFlag( DBO_TRX
);
56 * @internal This method is public so that it works with onTransactionResolution()
59 public function cancelOnRollback( $trigger ) {
60 if ( $trigger === IDatabase
::TRIGGER_ROLLBACK
) {
61 $this->callback
= null;
65 public function getOrigin() {
70 /** @deprecated class alias since 1.42 */
71 class_alias( AutoCommitUpdate
::class, 'AutoCommitUpdate' );