我遇到了以下错误:
Datasource names for all the database tags within the cftransaction tag must be the same.
这是由以下代码引起的:

transaction action="begin" {
  try {
    var data = {};

    data.time = getTickCount();

    addToLog("Persist", "Started persist operations");

    doClientPersist();
    cleanUp(arguments.importId);

    addToLog("Persist", "Completed the persist operations successfully", ((getTickCount()-data.time)/1000));

    return true;
  } catch (any e) {
    transactionRollback();
    data.error = e;
  }
}

该事务有效地将较低层方法的分配包装在doClientPersist()中。其中一个这样的调用位于我们的框架数据库抽象层的深处,它从一个单独的数据源(让我们说“邮政编码”数据源)中提取(选择)经度和纬度信息-该数据源严格是只读的。
<cffunction name="getLatitudeAndLongitude" access="package" returntype="query" output="false">
  <cfargument name="postcode" type="string" required="true" />
  <cfset var qPostcode = ''/>
  <cfquery name="qPostcode" datasource="postcodesDatasource">
    SELECT
      a.latitude,
      a.longitude
    FROM
      postcodes AS a
    WHERE
      a.postcode = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#postcode#"/>
  </cfquery>
  <cfreturn qPostcode/>
</cffunction>

<cffunction name="getPostcodeCoordinates" access="public" returntype="struct" output="false">
  <cfargument name="postcode" type="string" required="true"/>
  <cfscript>
    var data = {};

    data.postcode = getFormattedPostcode(arguments.postcode);
    data.valid    = isValidPostcode(data.postcode);
    data.coords   = {};

    if (data.valid) {
      data.query = getLatitudeAndLongitude(data.postcode);
      if (isQuery(data.query) && data.query.recordCount) {
        data.coords["latitude"]  = data.query["latitude"][1];
        data.coords["longitude"] = data.query["longitude"][1];
      } else if (data.valid == 2) {
        /** No match, try short postcode (RECURSIVE) **/
        data.coords = getPostcodeCoordinates(trim(left(data.postcode, len(data.postcode)-3)));
      }
    }
    return data.coords;
  </cfscript>
</cffunction>

阅读该问题后,文档会说以下内容:
In a transaction block, you can write queries to more than one database, but you must commit or roll back a transaction to one database before writing a query to another.
不幸的是,如上所述,获取此邮政编码数据的代码与实际的持久化操作完全无关,因为它执行的是无法更改的较低级别方法的网络,因此我无法在调用之前进行“顶级”事务远程数据源。

无论如何,我可以将“顶层”方法包装在事务中,并且仍然可以调用“邮政编码”数据源-对于我们来说,必须为每个客户端复制邮政编码信息是很愚蠢的,但是操作必须如果出现问题,请回退

提前致谢。

最佳答案

如我所见,您基本上有两种选择。

1)在交易之外查询您的数据。根据您应用程序的具体情况,这可能是将该方法移至事务处理块之前,拆分该方法,并将其一部分移至事务处理块之前,将数据预取到RAM中(将数据作为查询保存在内存中)。变量),然后让您的方法使用此预提取的数据,而不是直接查询数据库。

但是,所有这些解决方案的结果都是相同的。也就是说,SELECT查询本身是在事务之外执行的。

如果出于任何原因都不可行,请继续...

2)使用相同的数据源。请注意,您不必使用相同的数据库,而只需使用相同的数据源。因此,您可以在MySQL中使用database.tablename语法。

通过快速搜索,我找到了一个很好的例子:
Querying multiple databases at once

拥有比我更好的Google-fu的人可能很快就会想到更好的例子。

不过,基础知识是您在查询中使用FROM数据库名称而不是FROM表名称。

我相信这将要求数据库位于同一台MySQL服务器上。

关于mysql - 具有CFTRANSACTION的多个数据源,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15070506/

10-09 01:04
查看更多