Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 311 and 403] [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: 813 lines (27 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()
  getConnectionById()
  getConnection()
  makeValuesReferenced()
  setSessionVariable()
  executeSessionVariables()
  clusterExecute()
  isReadOnlyQuery()
  execute()
  __call()
  __get()
  __set()
  __clone()

ADOdbLoadBalancerConnection:: (4 methods):
  __construct()
  setConnectionTestCallback()
  getConnectionTestCallback()
  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

getConnectionById($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

setConnectionTestCallback($callback)   X-Ref
Anonymous function that is called and must return TRUE for the connection to be usable.*
The first argument is the type of connection to test.
Useful to check things like replication lag.

param: callable $callback
return: void

getConnectionTestCallback()   X-Ref

return: callable|null

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

return: bool