I'd say the KISS solution here is to connect to db1, run your query, disconnect, connect to db2, run your query, disconnect, and then reconnect to db1, and run the next query. The connection time isn't that long, and it's easier than trying to jump through a lot of hoops.
HOWEVER, I'm gonna guess that you're being bitten by a PHP peculiarity where if you do this:
$res1 = mysql_connect(connect args here);
$res2 = mysql_connect(same connect args here as above);
You'll get the same resource back for $res2 as you got for $res1 because the $params are the same.
So, when the connection times out, php is giving you back the same old connection resource. In which case, the fix is to use the fourth arg set true that says to make a new connection.
Hope that helps.