);
}
+ public function preCommitCallbacksPending() {
+ return $this->trxLevel && $this->trxPreCommitCallbacks;
+ }
+
/**
* @return string|null
*/
}
/**
- * Get the list of method names that have pending write queries or callbacks
- * for this transaction
+ * List the methods that have write queries or callbacks for the current transaction
*
- * @return array
+ * This method should not be used outside of Database/LoadBalancer
+ *
+ * @return string[]
+ * @since 1.32
*/
- protected function pendingWriteAndCallbackCallers() {
- if ( !$this->trxLevel ) {
- return [];
- }
-
- $fnames = $this->trxWriteCallers;
+ public function pendingWriteAndCallbackCallers() {
+ $fnames = $this->pendingWriteCallers();
foreach ( [
$this->trxIdleCallbacks,
$this->trxPreCommitCallbacks,
}
// Sanity check that no callbacks are dangling
- if (
- $this->trxIdleCallbacks || $this->trxPreCommitCallbacks || $this->trxEndCallbacks
- ) {
+ $fnames = $this->pendingWriteAndCallbackCallers();
+ if ( $fnames ) {
throw new RuntimeException(
- "Transaction callbacks are still pending:\n" .
- implode( ', ', $this->pendingWriteAndCallbackCallers() )
+ "Transaction callbacks are still pending:\n" . implode( ', ', $fnames )
);
}
if ( $ret === false ) {
if ( $this->trxLevel ) {
- if ( !$this->wasKnownStatementRollbackError() ) {
- # Either the query was aborted or all queries after BEGIN where aborted.
- if ( $this->explicitTrxActive() || $priorWritesPending ) {
- # In the first case, the only options going forward are (a) ROLLBACK, or
- # (b) ROLLBACK TO SAVEPOINT (if one was set). If the later case, the only
- # option is ROLLBACK, since the snapshots would have been released.
- $this->trxStatus = self::STATUS_TRX_ERROR;
- $this->trxStatusCause =
- $this->makeQueryException( $lastError, $lastErrno, $sql, $fname );
- $tempIgnore = false; // cannot recover
- } else {
- # Nothing prior was there to lose from the transaction,
- # so just roll it back.
- $this->rollback( __METHOD__ . " ($fname)", self::FLUSHING_INTERNAL );
- }
- $this->trxStatusIgnoredCause = null;
- } else {
+ if ( $this->wasKnownStatementRollbackError() ) {
# We're ignoring an error that caused just the current query to be aborted.
- # But log the cause so we can log a deprecation notice if a
- # caller actually does ignore it.
+ # But log the cause so we can log a deprecation notice if a caller actually
+ # does ignore it.
$this->trxStatusIgnoredCause = [ $lastError, $lastErrno, $fname ];
+ } else {
+ # Either the query was aborted or all queries after BEGIN where aborted.
+ # In the first case, the only options going forward are (a) ROLLBACK, or
+ # (b) ROLLBACK TO SAVEPOINT (if one was set). If the later case, the only
+ # option is ROLLBACK, since the snapshots would have been released.
+ $this->trxStatus = self::STATUS_TRX_ERROR;
+ $this->trxStatusCause =
+ $this->makeQueryException( $lastError, $lastErrno, $sql, $fname );
+ $tempIgnore = false; // cannot recover
+ $this->trxStatusIgnoredCause = null;
}
}
$this->trxEndCallbacks[] = [ $callback, $fname, $this->currentAtomicSectionId() ];
}
- final public function onTransactionIdle( callable $callback, $fname = __METHOD__ ) {
+ final public function onTransactionCommitOrIdle( callable $callback, $fname = __METHOD__ ) {
if ( !$this->trxLevel && $this->getTransactionRoundId() ) {
// Start an implicit transaction similar to how query() does
$this->begin( __METHOD__, self::TRANSACTION_INTERNAL );
}
}
+ final public function onTransactionIdle( callable $callback, $fname = __METHOD__ ) {
+ $this->onTransactionCommitOrIdle( $callback, $fname );
+ }
+
final public function onTransactionPreCommitOrIdle( callable $callback, $fname = __METHOD__ ) {
if ( !$this->trxLevel && $this->getTransactionRoundId() ) {
// Start an implicit transaction similar to how query() does
}
/**
- * Actually run and consume any "on transaction idle/resolution" callbacks.
+ * Actually consume and run any "on transaction idle/resolution" callbacks.
*
* This method should not be used outside of Database/LoadBalancer
*
* @param int $trigger IDatabase::TRIGGER_* constant
+ * @return int Number of callbacks attempted
* @since 1.20
* @throws Exception
*/
public function runOnTransactionIdleCallbacks( $trigger ) {
+ if ( $this->trxLevel ) { // sanity
+ throw new DBUnexpectedError( $this, __METHOD__ . ': a transaction is still open.' );
+ }
+
if ( $this->trxEndCallbacksSuppressed ) {
- return;
+ return 0;
}
+ $count = 0;
$autoTrx = $this->getFlag( self::DBO_TRX ); // automatic begin() enabled?
/** @var Exception $e */
$e = null; // first exception
$this->trxEndCallbacks = []; // consumed (recursion guard)
foreach ( $callbacks as $callback ) {
try {
+ ++$count;
list( $phpCallback ) = $callback;
$this->clearFlag( self::DBO_TRX ); // make each query its own transaction
call_user_func( $phpCallback, $trigger, $this );
if ( $e instanceof Exception ) {
throw $e; // re-throw any first exception
}
+
+ return $count;
}
/**
- * Actually run and consume any "on transaction pre-commit" callbacks.
+ * Actually consume and run any "on transaction pre-commit" callbacks.
*
* This method should not be used outside of Database/LoadBalancer
*
* @since 1.22
+ * @return int Number of callbacks attempted
* @throws Exception
*/
public function runOnTransactionPreCommitCallbacks() {
+ $count = 0;
+
$e = null; // first exception
do { // callbacks may add callbacks :)
$callbacks = $this->trxPreCommitCallbacks;
$this->trxPreCommitCallbacks = []; // consumed (and recursion guard)
foreach ( $callbacks as $callback ) {
try {
+ ++$count;
list( $phpCallback ) = $callback;
call_user_func( $phpCallback, $this );
} catch ( Exception $ex ) {
if ( $e instanceof Exception ) {
throw $e; // re-throw any first exception
}
+
+ return $count;
}
/**
$savepointId = $cancelable === self::ATOMIC_CANCELABLE ? self::$NOT_APPLICABLE : null;
if ( !$this->trxLevel ) {
- $this->begin( $fname, self::TRANSACTION_INTERNAL );
+ $this->begin( $fname, self::TRANSACTION_INTERNAL ); // sets trxAutomatic
// If DBO_TRX is set, a series of startAtomic/endAtomic pairs will result
// in all changes being in one transaction to keep requests transactional.
if ( $this->getFlag( self::DBO_TRX ) ) {
);
}
- $this->runOnTransactionIdleCallbacks( self::TRIGGER_COMMIT );
- $this->runTransactionListenerCallbacks( self::TRIGGER_COMMIT );
+ // With FLUSHING_ALL_PEERS, callbacks will be explicitly run later
+ if ( $flush !== self::FLUSHING_ALL_PEERS ) {
+ $this->runOnTransactionIdleCallbacks( self::TRIGGER_COMMIT );
+ $this->runTransactionListenerCallbacks( self::TRIGGER_COMMIT );
+ }
}
/**
$this->trxIdleCallbacks = [];
$this->trxPreCommitCallbacks = [];
- if ( $trxActive ) {
+ // With FLUSHING_ALL_PEERS, callbacks will be explicitly run later
+ if ( $trxActive && $flush !== self::FLUSHING_ALL_PEERS ) {
try {
$this->runOnTransactionIdleCallbacks( self::TRIGGER_ROLLBACK );
} catch ( Exception $e ) {
}
}
-class_alias( Database::class, 'DatabaseBase' ); // b/c for old name
-class_alias( Database::class, 'Database' ); // b/c global alias
+/**
+ * @deprecated since 1.28
+ */
+class_alias( Database::class, 'DatabaseBase' );
+
+/**
+ * @deprecated since 1.29
+ */
+class_alias( Database::class, 'Database' );