From: Aaron Schulz Date: Sat, 6 Jul 2013 06:19:11 +0000 (-0700) Subject: database: added DBConnRef wrapper to manage calling reuseConnection() X-Git-Tag: 1.31.0-rc.0~19189^2 X-Git-Url: http://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/operations/recherche.php?a=commitdiff_plain;h=d921b2fd7e3aaf0494f7f3a991a7e7813a7461ad;p=lhc%2Fweb%2Fwiklou.git database: added DBConnRef wrapper to manage calling reuseConnection() Change-Id: Ifc9db62d1ac34d0c6a072d6f2d05bdcf73af14bb --- diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 6f8cd4bc9c..2f0ac23c51 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -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', diff --git a/includes/db/LoadBalancer.php b/includes/db/LoadBalancer.php index db709b523f..ab200b4a9a 100644 --- a/includes/db/LoadBalancer.php +++ b/includes/db/LoadBalancer.php @@ -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 ); + } +}