CodeIgniter Forums
Peristent connect when using multiple databases with the same login credentials - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Peristent connect when using multiple databases with the same login credentials (/showthread.php?tid=31965)



Peristent connect when using multiple databases with the same login credentials - El Forum - 07-08-2010

[eluser]dignick[/eluser]
After a lot of investigation here I have found that if you have two database objects which use the same database login credentials but different databases, and you have persistent connect enabled for both connections, then problems occur when trying to run queries on the database you initialised first.
Example:
Code:
$test1['hostname'] = "mysql.server.com";
$test1['username'] = "user";
$test1['password'] = "pass";
$test1['pconnect'] = TRUE;
$test1['database'] = "test1";

...

$test2 = $test1;
$test2['database'] = "test2";

...

$db1 = $this->load->database($test1, TRUE);
$db2 = $this->load->database($test2, TRUE);
        
// DB2
$db2->set('test', 2);
$db2->where('id', 0);
$db2->update('test2');
        
// DB1
$db1->set('test', 1);
$db1->where('id', 0);
$db1->update('test1');

Because persistent connect will not return a unique connection id, and the database is only selected when the database object is initialised, then when the $db1 object runs a query its trying to run it on 'test2'. See here for some testing of this.

I wrote a solution. While not ideal, it works.