ADOdb

Database Abstraction Layer for PHP

User Tools

Site Tools


v5:reference:loadbalancer:getconnection

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
Last revisionBoth sides next revision
v5:reference:loadbalancer:getconnection [2017/04/28 00:37] – created mnewnhamv5:reference:loadbalancer:getconnection [2017/05/11 00:49] – [Usage] mnewnham
Line 16: Line 16:
 The method ''getConnection()'' gets a connection of the requested type ''(write/readonly)'' and ensures that the connection is established and all initialization is complete. The method ''getConnection()'' gets a connection of the requested type ''(write/readonly)'' and ensures that the connection is established and all initialization is complete.
  
-The returned object is an [[v5:reference:connection:newadoconnection|ADOdb connection object]], which can be accessed in the normal way+The returned object is an [[v5:reference:connection:adonewconnection|ADOdb connection object]], which can be accessed in the normal way
  
 --------------------------- ---------------------------
 +<WRAP tip>
 +To ensure the accuracy of the response, you should have already executed at least one statement against the load balancer connection, or used a method such as **clusterExecute**
 +</WRAP>
 ===== Usage ===== ===== Usage =====
 +If the system could not determine whether the command to be executed is read-only or not, for example in a stored procedure, then this method can be used to retrieve a connection of the appropriate type.
 <code php> <code php>
 +$db = new ADOdbLoadBalancer;
 + 
 +$driver = 'mysqli';
 +$database = 'employees';
 +$user     = 'adodb';
 +$password = 'adodb';
 +$host     = '192.168.0.200';
 +$db0 = new ADOdbLoadBalancerConnection( $driver,
 +                                        'write', 
 +                                        10, 
 +                                        false, 
 +                                        $host, 
 +                                        $user, 
 +                                        $password, 
 +                                        $database );
 +/*
 +* Add the connection to the pool
 +*/
 +$db->addConnection( $db0 );
 +$db1 = new ADOdbLoadBalancerConnection( $driver,
 +                                        'readonly', 
 +                                        10, 
 +                                        false, 
 +                                        $host, 
 +                                        $user, 
 +                                        $password, 
 +                                        $database );
 +/*
 +* Add the connection to the pool
 +*/
 +$db->addConnection( $db1 );
 +
 +/*
 +* Execute something to ensure connection accuracy
 +*/
 +$db->clusterExecute('SET SESSION 1=1');
 +
 +/*
 +* Find a readwrite connection, pin it for the next statement
 +*/
 +$db->getConnection('write',$pin=true);
 +
 +/*
 +* Now construct a stored procedure and execute it against $db
 +*/
 </code> </code>
  
v5/reference/loadbalancer/getconnection.txt · Last modified: 2017/05/11 00:51 by mnewnham