Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

ADOdb Load Balancer ADOdbLoadBalancer is a class that allows the user to do read/write splitting and load balancing across multiple servers. It can handle and load balance any number of write capable (AKA: master) or readonly (AKA: slave) connections, including dealing with connection failures and retrying queries on a different connection instead.

Author: Mike Benoit
Copyright: (c) 2016 Mike Benoit and the ADOdb community
License: BSD-3-Clause
License: GNU Lesser General Public License (LGPL) v2.1 or later
Version: v5.21.0 2021-02-27
File Size: 773 lines (25 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 2 classes

ADOdbLoadBalancer:: (17 methods):
  setSessionInitSQL()
  addConnection()
  removeConnection()
  getConnectionByWeight()
  getLoadBalancedConnection()
  _getConnection()
  getConnection()
  makeValuesReferenced()
  setSessionVariable()
  executeSessionVariables()
  clusterExecute()
  isReadOnlyQuery()
  execute()
  __call()
  __get()
  __set()
  __clone()

ADOdbLoadBalancerConnection:: (2 methods):
  __construct()
  getADOdbObject()


Class: ADOdbLoadBalancer  - X-Ref

Class ADOdbLoadBalancer

setSessionInitSQL($sql)   X-Ref
Defines SQL queries that are executed each time a new database connection is established.

param: $sql
return: bool

addConnection($obj)   X-Ref
Adds a new database connection to the pool, but no actual connection is made until its needed.

param: $obj
return: bool

removeConnection($i)   X-Ref
Removes a database connection from the pool.

param: $i
return: bool

getConnectionByWeight($type)   X-Ref
Returns a database connection of the specified type.

Takes into account the connection weight for load balancing.

param: string $type Type of database connection, either: 'write' capable or 'readonly'
return: bool|int|string

getLoadBalancedConnection($type)   X-Ref
Returns the proper database connection when taking into account sticky sessions and load balancing.

param: $type
return: bool|int|mixed|string

_getConnection($connection_id)   X-Ref
Returns the ADODB connection object by connection_id.

Ensures that it's connected and the session variables are executed.

param: $connection_id
return: bool|ADOConnection

getConnection($type = 'write', $pin_connection = null)   X-Ref
Returns the ADODB connection object by database type.

Ensures that it's connected and the session variables are executed.

param: string $type
param: null   $pin_connection
return: ADOConnection|bool

makeValuesReferenced($arr)   X-Ref
This is a hack to work around pass by reference error.

Parameter 1 to ADOConnection::GetInsertSQL() expected to be a reference,
value given in adodb-loadbalancer.inc.php on line 83

param: $arr
return: array

setSessionVariable($name, $value, $execute_immediately = true)   X-Ref
Allow setting session variables that are maintained across connections.

Its important that these are set using name/value, so it can determine
if the same variable is set multiple times causing bloat/clutter when
new connections are established. For example if the time_zone is set to
many different ones through the course of a single connection, a new
connection should only set it to the most recent value.

param: $name
param: $value
param: bool  $execute_immediately
return: array|bool|mixed

executeSessionVariables($adodb_obj = false)   X-Ref
Executes the session variables on a given ADODB object.

param: ADOConnection|bool $adodb_obj
return: array|bool|mixed

clusterExecute($sql,$inputarr = false,$return_all_results = false,$existing_connections_only = true)   X-Ref
Executes the same SQL QUERY on the entire cluster of connections.
Would be used for things like SET SESSION TIME ZONE calls and such.

param: $sql
param: bool $inputarr
param: bool $return_all_results
param: bool $existing_connections_only
return: array|bool|mixed

isReadOnlyQuery($sql)   X-Ref
Determines if a SQL query is read-only or not.

param: string $sql SQL Query to test.
return: bool

execute($sql, $inputarr = false)   X-Ref
Use this instead of __call() as it significantly reduces the overhead of call_user_func_array().

param: $sql
param: bool $inputarr
return: array|bool|mixed

__call($method, $args)   X-Ref
Magic method to intercept method and callback to the proper ADODB object for write/readonly connections.

param: string $method ADODB method to call.
param: array  $args   Arguments to the ADODB method.
return: bool|mixed

__get($property)   X-Ref
Magic method to proxy property getter calls back to the proper ADODB object currently in use.

param: $property
return: mixed

__set($property, $value)   X-Ref
Magic method to proxy property setter calls back to the proper ADODB object currently in use.

param: $property
param: $value
return: mixed

__clone()   X-Ref
Override the __clone() magic method.


Class: ADOdbLoadBalancerConnection  - X-Ref

Class ADOdbLoadBalancerConnection

__construct($driver,$type = 'write',$weight = 1,$persistent_connection = false,$argHostname = '',$argUsername = '',$argPassword = '',$argDatabaseName = '')   X-Ref
ADOdbLoadBalancerConnection constructor to setup the ADODB object.

param: $driver
param: string $type
param: int    $weight
param: bool   $persistent_connection
param: string $argHostname
param: string $argUsername
param: string $argPassword
param: string $argDatabaseName

getADOdbObject()   X-Ref
Returns the ADODB object for this connection.

return: bool