database: added DBConnRef wrapper to manage calling reuseConnection()
authorAaron Schulz <aschulz@wikimedia.org>
Sat, 6 Jul 2013 06:19:11 +0000 (23:19 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Mon, 15 Jul 2013 22:10:45 +0000 (15:10 -0700)
Change-Id: Ifc9db62d1ac34d0c6a072d6f2d05bdcf73af14bb

includes/AutoLoader.php
includes/db/LoadBalancer.php

index 6f8cd4b..2f0ac23 100644 (file)
@@ -483,6 +483,7 @@ $wgAutoloadLocalClasses = array(
        'DatabaseType' => 'includes/db/Database.php',
        'DBAccessError' => 'includes/db/LBFactory.php',
        'DBConnectionError' => 'includes/db/DatabaseError.php',
+       'DBConnRef' => 'includes/db/LoadBalancer.php',
        'DBError' => 'includes/db/DatabaseError.php',
        'DBObject' => 'includes/db/DatabaseUtility.php',
        'IORMRow' => 'includes/db/IORMRow.php',
index db709b5..ab200b4 100644 (file)
@@ -545,6 +545,22 @@ class LoadBalancer {
                }
        }
 
+       /**
+        * Get a database connection handle reference
+        *
+        * The handle's methods wrap simply wrap those of a DatabaseBase handle
+        *
+        * @see LoadBalancer::getConnection() for parameter information
+        *
+        * @param integer $db
+        * @param mixed $groups
+        * @param string $wiki
+        * @return DBConnRef
+        */
+       public function getConnectionRef( $db, $groups = array(), $wiki = false ) {
+               return new DBConnRef( $this, $this->getConnection( $db, $groups, $wiki ) );
+       }
+
        /**
         * Open a connection to the server given by the specified index
         * Index must be an actual index into the array.
@@ -1068,3 +1084,33 @@ class LoadBalancer {
                $this->mLagTimes = null;
        }
 }
+
+/**
+ * Helper class to handle automatically marking connectons as reusable (via RAII pattern)
+ *
+ * @ingroup Database
+ * @since 1.22
+ */
+class DBConnRef {
+       /** @var LoadBalancer */
+       protected $lb;
+       /** @var DatabaseBase */
+       protected $conn;
+
+       /**
+        * @param $lb LoadBalancer
+        * @param $conn DatabaseBase
+        */
+       public function __construct( LoadBalancer $lb, DatabaseBase $conn ) {
+               $this->lb = $lb;
+               $this->conn = $conn;
+       }
+
+       public function __call( $name, $arguments ) {
+               return call_user_func_array( array( $this->conn, $name ), $arguments );
+       }
+
+       function __destruct() {
+               $this->lb->reuseConnection( $this->conn );
+       }
+}