Home » Database » MDB2_Driver_mssql » Bug #9735
Two instances fail to keep different databases selected
Details
| Submitted | 2007-01-04 20:32 UTC |
|---|---|
| From | pete at digitalhill dot com |
| Assigned | quipo |
| Status | Closed |
| Package | MDB2_Driver_mssql |
| PHP Version | 4.4.4 |
| OS | Linux |
| Roadmaps | (Not assigned) |
Comments
[2007-01-04 20:32 UTC] pete at digitalhill dot com
Description:
------------
Two separate instances of a MDB2 object end up using the same connection resource when created. This results in the database name check in MDB2_Driver_mssql::_do_query() failing to notice that the currently selected database is no longer selected when one alternates queries from the two objects. This naturally results in tables not being found and the like.
Test script:
---------------
//approximation of the problem
require_once('PEAR.php');
require_once('MDB2.php');
$dsn_one['username'] = "foo";
$dsn_one['password'] = "bar";
$dsn_one['hostspec'] = "remote_machine";
$dsn_one['phptype'] = 'mssql';
$db_one =& MDB2::factory($dsn_one); //connect
$db_one->setDatabase('database_one'); //select one database for this connection
$db_one->setFetchMode(MDB2_FETCHMODE_ASSOC); //we like hashes, ordered arrays require thinking
$dsn_two['username'] = "foo";
$dsn_two['password'] = "bar";
$dsn_two['hostspec'] = "remote_machine";
$dsn_two['phptype'] = 'mssql';
$db_two =& MDB2::factory($dsn_two); //connect
$db_two->setDatabase('database_two'); //select two database for this connection
$db_two->setFetchMode(MDB2_FETCHMODE_ASSOC); //we like hashes, ordered arrays require thinking
$res1 = $db_one->query("SELECT * FROM table_in_database_one WHERE id = 1"); //works
$res2 = $db_two->query("SELECT * FROM table_in_database_two WHERE other_id = 2"); //works, as db_two still has '' for variable connection_database_name and so it performs the mssql_select_db
$res1 = $db_one->query("SELECT * FROM table_in_database_one WHERE id = 3"); //fails
//my fix was to replace line 289:
$this->connected_database_name = '';
//with two lines:
if(!isset($GLOBALS['__MDB2_mssql_connection_database_name'])) { $GLOBALS['__MDB2_mssql_connection_database_name'] = ''; }
$this->connected_database_name =& $GLOBALS['__MDB2_mssql_connection_database_name'];
//using a global might not be the best solution, a static variable or method might be better, but this solved my immediate issue.
Expected result:
----------------
I would expect that the connections would track the selected database successfully, or just use different connections (although there is a limit).
Admittedly the situation isn't ideal data organization, but sometimes things just end up that way, so we have to deal with it.