Ale*_*exP 5 mysql coldfusion transactions coldfusion-9
我遇到了以下错误:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
该事务有效地将分配的较低级别方法包装在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>
Run Code Online (Sandbox Code Playgroud)
阅读这个问题,文档说如下:
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语法。
通过快速搜索,我找到了一个很好的例子: 同时查询多个数据库
比我更懂 Google 的人可能很快就能想出更好的例子。
但基础知识是在查询中使用 FROM database.tablename 而不是仅使用 FROM tablename。
不过,我相信这需要数据库位于同一台 MySQL 服务器上。
| 归档时间: |
|
| 查看次数: |
1943 次 |
| 最近记录: |