Search code examples
coldfusioniis-6coldfusion-11

ColdFusion site sometimes uses wrong database


I am working on two ColdFusion sites (say "Site A" and "Site B") that are setup on the same server using IIS. Both sites have the same source code, but different URL's and data sources. "Site A" uses database DB1 and "Site B" uses DB2.

The issue is, sometimes I noticed that "Site B" is using DB1 instead of DB2. This issue doesn't appear every time. For example if I hit a page 10 times, about 7 times it uses the correct database and 3 times the wrong database. This issue is not with "Site A", only with "Site B".

I tried many solution like recreating the data sources, dumping the database on each step, but have not found the exact reason. Is any one else facing this issue? Do I need to update ColdFusion?


Solution

  • (Expanded from comments ....)

    First guess would be something in the code is pointing to the wrong place. How are you referencing the datasource in CF code (datasource setting in the Application.cfc)? Do the 2 Application.cfc files have different application names? They should. Otherwise, both applications will share the same settings. Since the Application.cfc executes at the start of each request, that could produce race conditions as multiple threads may read and write the shared datasource setting at the same time.

    Example:

    1. "John" requests page in SiteA. SiteA's Application.cfc executes. The application datasource is set to DatasourceA;

      component {
         this.name = "MyApplication";
         this.datasource = "DatasourceA";
      }
      
    2. At the same time, "Mike" requests a page in SiteB. SiteB's Application.cfc executes. The application datasource is changed to DatasourceB

      component {
         this.name = "MyApplication";
         this.datasource = "DatasourceB";
      }
      
    3. Then John's request on SiteA runs a query. Since the application datasource is now DatasourceB, the request queries the wrong database.

    The solution is to use different application names in the Application.cfc.