当访问多个数据库时,Coldfusion cftransaction标记的行为是什么? [英] What is the behavior of Coldfusion cftransaction tag when multiple databases are accessed?

查看:90
本文介绍了当访问多个数据库时,Coldfusion cftransaction标记的行为是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

coldfusion文档,(我使用的是CF8)指出:

The coldfusion documentation, (I'm using CF8) states:


更改了请求的数据
在事务块
内的所有操作成功执行之前,查询不会提交到数据源。

Changes to data that is requested by the queries are not committed to the datasource until all actions within the transaction block have executed successfully.

但是它也指出:


在事务块中,您可以向多个数据库写入查询,但是必须在向另一个数据库写入查询之前将事务提交或回滚到一个数据库

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

我的代码库中有多个事务,这些事务访问2个数据库以进行选择和更新/插入。该代码假定所有查询都将成功或全部回滚。但是基于文档中的这一行,我不知道这是否成立:但是您必须先向一个数据库提交事务或回滚事务,然后再向另一个数据库写查询。

I have multiple transactions in my code base which access 2 databases for both selects and update/inserts. The code assumes that all queries will either succeed or they will all be rolled back. But I don't know if that is true based upon the line in the docs that says: "but you must commit or roll back a transaction to one database before writing a query to another".

如果对第一个数据库的写入成功,然后对另一个数据库的后续写入失败,该怎么办?会先回滚吗?

What is the behavior if a write to the first database succeeds, then the subsequent write to another database fails? Will the first be rolled back?

推荐答案

文档的含义是必须放置< cftransaction action = commit> ; 查询到一个数据库之后,您便可以继续使用另一个数据源。如果它检测到您在事务内部使用了不同数据源的< cfquery> 标签,而没有使用提交,它将引发错误。请参阅数据库文档以获取确切的事务支持,因为通过数据库驱动程序的CFML仅代表您发送事务命令,它不负责其执行或行为。

What the documentation means is that you must put a <cftransaction action="commit"> after the queries to one database before you can move on to using another datasource. It will throw an error if it detects that you have <cfquery> tags with different datasources inside of a transaction without using the commit. See your database documentation for exact transaction support as the CFML via the database driver is only sending in transaction commands on your behalf, it is not responsible for their execution or behavior. Enable JDBC logging in your database to see this in action.

不起作用:

  <cftransaction action="begin">

    <cfquery datasource="foo">
    select * from foo_test
    </cfquery>

    <cfquery datasource="bar">
    select * from bar_test
    </cfquery>

  </cftransaction>

工作会

<cftransaction action="begin">

    <cfquery datasource="foo">
    select * from foo_test
    </cfquery>

  <cftransaction action="commit"><!-- Commit before switching DSNs --->

   <cfquery datasource="bar">
    select * from bar_test
    </cfquery>

</cftransaction>

如果您使用三个部件名称通过单个数据源进行多个数据库访问,则事务控制将起作用。

If you are using three part names for multiple database access through a single datasource, the transaction control will work.

<cftransaction action="begin">

    <cfquery datasource="foo">
    INSERT INTO foo_test ( id )
    VALUES ( 70 )
    </cfquery>

    <!-- insert into the bar database via the foo datasource --->
    <cfquery datasource="foo">
    INSERT INTO bar.dbo.bar_test (id )
    VALUES ( 'frank' ) <!-- Fails because not an int and the prior insert into foo is rolled back -->
    </cfquery>

</cftransaction>

这篇关于当访问多个数据库时,Coldfusion cftransaction标记的行为是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆