#------------------------------------------------------------------------------
protected $mLastQuery = '';
+ protected $mDoneWrites = false;
protected $mPHPError = false;
protected $mServer, $mUser, $mPassword, $mConn = null, $mDBname;
* @return String
*/
function lastQuery() { return $this->mLastQuery; }
-
+
+
+ /**
+ * Returns true if the connection may have been used for write queries.
+ * Should return true if unsure.
+ */
+ function doneWrites() { return $this->mDoneWrites; }
+
/**
* Is a connection to the database open?
* @return Boolean
}
}
+ /**
+ * Determine whether a query writes to the DB.
+ * Should return true if unsure.
+ */
+ function isWriteQuery( $sql ) {
+ return !preg_match( '/^(?:SELECT|BEGIN|COMMIT|SET|SHOW)\b/i', $sql );
+ }
+
/**
* Usually aborts on failure. If errors are explicitly ignored, returns success.
*
}
$this->mLastQuery = $sql;
+ if ( !$this->mDoneWrites && $this->isWriteQuery( $sql ) ) {
+ // Set a flag indicating that writes have been done
+ wfDebug( __METHOD__.": Writes done: $sql\n" );
+ $this->mDoneWrites = true;
+ }
# Add a comment for easy SHOW PROCESSLIST interpretation
#if ( $fname ) {
* @param LoadBalancer $lb
*/
function shutdownLB( $lb ) {
- if ( session_id() != '' && $lb->getServerCount() > 1 ) {
- $masterName = $lb->getServerName( 0 );
- if ( !isset( $this->shutdownPos[$masterName] ) ) {
- $pos = $lb->getMasterPos();
- $info = $lb->parentInfo();
- wfDebug( __METHOD__.": LB " . $info['id'] . " has master pos $pos\n" );
- $this->shutdownPos[$masterName] = $pos;
- }
+ // Don't start a session, don't bother with non-replicated setups
+ if ( strval( session_id() ) == '' || $lb->getServerCount() <= 1 ) {
+ return;
+ }
+ $masterName = $lb->getServerName( 0 );
+ if ( isset( $this->shutdownPos[$masterName] ) ) {
+ // Already done
+ return;
+ }
+ // Only save the position if writes have been done on the connection
+ $db = $lb->getAnyOpenConnection( 0 );
+ $info = $lb->parentInfo();
+ if ( !$db || !$db->doneWrites() ) {
+ wfDebug( __METHOD__.": LB {$info['id']}, no writes done\n" );
+ return;
}
+ $pos = $db->getMasterPos();
+ wfDebug( __METHOD__.": LB {$info['id']} has master pos $pos\n" );
+ $this->shutdownPos[$masterName] = $pos;
}
/**