Might just be mysql context, but it seems that when
array_search($previous_db, $db_conns)
finds the first 'equal' connection, it always finds default even when different db's are connected.

This isn't a problem when only one switch occurs, because the db to switch back to is always 'default', but if two set_active_db frames are nested, then the inner call never switches back to the middle db context.

The attached patch fixes this by explicitly storing the previous name.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

schuyler1d’s picture

FileSize
1.14 KB

sorry, this one's better

drumm’s picture

Priority: Critical » Normal
Status: Needs review » Needs work

I don't think this is critical since not many people connect to more than two databases.

Please use diff -uF. For more information, see http://drupal.org/patch/create.

I think the code might be okay, but I'm not used to reading patches formatted this way.

schuyler1d’s picture

FileSize
768 bytes

sorry for the badly formatted patch. This one should be better.

schuyler1d’s picture

just for the record, I had marked it critical, because it came up using module Multisite Manager--basically any time something switches active_db and makes a call to watchdog() or anything that calls watchdog() ends up losing its db context.

schuyler1d’s picture

Status: Needs work » Needs review

oops, forgot to change the status

nedjo’s picture

Status: Needs review » Needs work

This indeed looks like a bug. The problem if I understand it correctly is that the array of connections we're caching is really an array of resource db link identifiers. Assuming two schemas on the same db server, the resource is the same; it's the same connection to a db server, all we've done is switch active databases.

The proposed solution looks good.

This line is now superfluous and so should be deleted:


   $previous_db = $active_db;

schuyler1d’s picture

Status: Needs work » Needs review
FileSize
843 bytes

true, updated patch removes superfluous line.

Nedjo's description is correct as I understand it, as well.

schuyler1d’s picture

Status: Needs review » Reviewed & tested by the community

trying to ping core...

Gábor Hojtsy’s picture

schuyler1d also opened an issue for 6.x, which I marked as duplicate of this one.

I looked into this as well, and what the current code does is basically looking for the resource identifier in the resource list, looking up the key used for that. So the question is when new database connection resource identifiers are created in PHP. This could be database dependent even, a new connection with the same user/pass might return a new resource ID, but it is more likely that it will return the same.

drumm’s picture

Status: Reviewed & tested by the community » Needs work

The initial $active_name should be set to FALSE to match the documentation.

Otherwise, I think this is okay.

schuyler1d’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
839 bytes

thanks for that catch, fixed.

Gábor Hojtsy’s picture

Committed to 6.x. Still needs to be committed to 5.x.

drumm’s picture

Version: 5.x-dev » 4.7.x-dev

Committed to 5.x.

Cleanly applies to 4.7.x.

killes@www.drop.org’s picture

Status: Reviewed & tested by the community » Fixed

applied

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.