Where are templates from hook_theme stored in the database
Normally, db_set_active() is used to query external data.  I have never heard about it being used to switch between two full Drupal databases from within the same active codebase / website.

I suspect one or two things is happening: you are having cache problems and/or you are having registry problems.

When you clear caches via the UI or drush, the full registry gets rebuilt, which includes theme functions and their template files.  That information will get stored in whatever database is active at the time.

If you switch databases, the cache and registry will be pulled from the newly active database.   In addition, any new cache entries will be written to the new database.

So, I think your problem is that the wrong cache and/or the wrong registry is being queried when you use the Drupal API and you are getting stale data.

The proper way to accomplish this is to set up a dev/staging/live environment, where code goes forward from dev -> staging -> live, and databases go backwards from live -> staging -> dev.  There are lots of articles on the net, and a few questions here, about how to do this.