Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 311 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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.

Copyright: 2016 Mike Benoit and the ADOdb community
License: BSD-3-Clause
License: LGPL-2.1-or-later
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.

return: bool
param: $sql

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

return: bool
param: $obj

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

return: bool
param: $i

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

Takes into account the connection weight for load balancing.

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

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

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

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

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

return: bool|ADOConnection
param: $connection_id

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.

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

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

return: array
param: $arr

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.

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

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

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

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.

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

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

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

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

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

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

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

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

return: mixed
param: $property

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

return: mixed
param: $property
param: $value

__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