X-Git-Url: http://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/exercices/supprimer.php?a=blobdiff_plain;f=includes%2Fdb%2FLoadBalancer.php;h=60c283304a477084307641d4854156f08ca7c8e4;hb=ebbea3c974312c56e93799fc93ee3f35130703f9;hp=1d273085fe793861c49fae09854136b9cecaf148;hpb=af154bea2bd58112674c4dd71acc396152ab3653;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/db/LoadBalancer.php b/includes/db/LoadBalancer.php index 1d273085fe..60c283304a 100644 --- a/includes/db/LoadBalancer.php +++ b/includes/db/LoadBalancer.php @@ -117,7 +117,7 @@ class LoadBalancer { * Given an array of non-normalised probabilities, this function will select * an element and return the appropriate key * - * @deprecated 1.21, use ArrayUtils::pickRandom() + * @deprecated since 1.21, use ArrayUtils::pickRandom() * * @param $weights array * @@ -479,6 +479,7 @@ class LoadBalancer { # Operation-based index if ( $i == DB_SLAVE ) { + $this->mLastError = 'Unknown error'; // reset error string $i = $this->getReaderIndex( false, $wiki ); # Couldn't find a working server in getReaderIndex()? if ( $i === false ) { @@ -544,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 +1085,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 implements IDatabase { + /** @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 ); + } +}