database: Small DB class cleanups
authorAaron Schulz <aschulz@wikimedia.org>
Thu, 25 Jun 2015 00:24:00 +0000 (17:24 -0700)
committerKrinkle <krinklemail@gmail.com>
Sun, 28 Jun 2015 00:15:54 +0000 (00:15 +0000)
* Moved some duplicated logic to assertOpen() method
* Override doc type for mConn for the mysqli subclass
* Fixed a few code comments

Change-Id: I78d595554ed51f64ca7cf7bd7ce369a492a59145

includes/db/Database.php
includes/db/DatabaseMysql.php
includes/db/DatabaseMysqlBase.php
includes/db/DatabaseMysqli.php

index 94cf1f2..032e926 100644 (file)
@@ -1019,6 +1019,17 @@ abstract class DatabaseBase implements IDatabase {
                return $closed;
        }
 
+       /**
+        * Make sure isOpen() returns true as a sanity check
+        *
+        * @throws DBUnexpectedError
+        */
+       protected function assertOpen() {
+               if ( !$this->isOpen() ) {
+                       throw new DBUnexpectedError( $this, "DB connection was already closed." );
+               }
+       }
+
        /**
         * Closes underlying database connection
         * @since 1.20
@@ -1175,9 +1186,7 @@ abstract class DatabaseBase implements IDatabase {
                $queryId = MWDebug::query( $sql, $fname, $isMaster );
 
                # Avoid fatals if close() was called
-               if ( !$this->isOpen() ) {
-                       throw new DBUnexpectedError( $this, "DB connection was already closed." );
-               }
+               $this->assertOpen();
 
                # Do the query and handle errors
                $startTime = microtime( true );
@@ -3648,9 +3657,7 @@ abstract class DatabaseBase implements IDatabase {
                }
 
                # Avoid fatals if close() was called
-               if ( !$this->isOpen() ) {
-                       throw new DBUnexpectedError( $this, "DB connection was already closed." );
-               }
+               $this->assertOpen();
 
                $this->doBegin( $fname );
                $this->mTrxTimestamp = microtime( true );
@@ -3715,9 +3722,7 @@ abstract class DatabaseBase implements IDatabase {
                }
 
                # Avoid fatals if close() was called
-               if ( !$this->isOpen() ) {
-                       throw new DBUnexpectedError( $this, "DB connection was already closed." );
-               }
+               $this->assertOpen();
 
                $this->runOnTransactionPreCommitCallbacks();
                $writeTime = $this->pendingWriteQueryDuration();
@@ -3774,9 +3779,7 @@ abstract class DatabaseBase implements IDatabase {
                }
 
                # Avoid fatals if close() was called
-               if ( !$this->isOpen() ) {
-                       throw new DBUnexpectedError( $this, "DB connection was already closed." );
-               }
+               $this->assertOpen();
 
                $this->doRollback( $fname );
                $this->mTrxIdleCallbacks = array(); // cancel
index 823d9b6..9153753 100644 (file)
@@ -73,6 +73,9 @@ class DatabaseMysql extends DatabaseMysqlBase {
 
                $conn = false;
 
+               # The kernel's default SYN retransmission period is far too slow for us,
+               # so we use a short timeout plus a manual retry. Retrying means that a small
+               # but finite rate of SYN packet loss won't cause user-visible errors.
                for ( $i = 0; $i < $numAttempts && !$conn; $i++ ) {
                        if ( $i > 1 ) {
                                usleep( 1000 );
index a189648..9285d70 100644 (file)
@@ -59,22 +59,16 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
        function open( $server, $user, $password, $dbName ) {
                global $wgAllDBsAreLocalhost, $wgSQLMode;
 
-               # Debugging hack -- fake cluster
-               if ( $wgAllDBsAreLocalhost ) {
-                       $realServer = 'localhost';
-               } else {
-                       $realServer = $server;
-               }
+               # Close/unset connection handle
                $this->close();
+
+               # Debugging hack -- fake cluster
+               $realServer = $wgAllDBsAreLocalhost ? 'localhost' : $server;
                $this->mServer = $server;
                $this->mUser = $user;
                $this->mPassword = $password;
                $this->mDBname = $dbName;
 
-               # The kernel's default SYN retransmission period is far too slow for us,
-               # so we use a short timeout plus a manual retry. Retrying means that a small
-               # but finite rate of SYN packet loss won't cause user-visible errors.
-               $this->mConn = false;
                $this->installErrorHandler();
                try {
                        $this->mConn = $this->mysqlConnect( $realServer );
index d2b5ecb..d4106be 100644 (file)
@@ -29,6 +29,9 @@
  * @see Database
  */
 class DatabaseMysqli extends DatabaseMysqlBase {
+       /** @var mysqli */
+       protected $mConn;
+
        /**
         * @param string $sql
         * @return resource