Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created September 8, 2014 10:53
Show Gist options
  • Save bennadel/fa9ffd84bc74c6f16b10 to your computer and use it in GitHub Desktop.
Save bennadel/fa9ffd84bc74c6f16b10 to your computer and use it in GitHub Desktop.
Be Careful About Launching Asynchronous Tasks Inside CFTransaction In ColdFusion
<!--- Reset out data table so we know that we're only dealing with one record. --->
<cfquery name="reset" datasource="testing">
TRUNCATE TABLE friend;
</cfquery>
<cftransaction action="begin">
<!--- Insert our test record. --->
<cfquery name="insert" datasource="testing">
INSERT INTO friend
(
id,
name
) VALUES (
<cfqueryparam value="1" cfsqltype="cf_sql_integer" />,
<cfqueryparam value="Tricia" cfsqltype="cf_sql_varchar" />
);
</cfquery>
<!--- At this point, we're still inside the Transaction. --->
<cfquery name="preThread" datasource="testing">
SELECT
id,
name
FROM
friend
;
</cfquery>
<cfthread name="async">
<!---
At this point, we're no longer inside the Transaction. And, we can't read
the record that has not yet been committed.
--->
<cfquery name="thread.friends" datasource="testing">
SELECT
id,
name
FROM
friend
;
</cfquery>
</cfthread>
<!--- Make sure the thread finishes executing before the transaction is closed. --->
<cfthread action="join" />
</cftransaction>
<!--- Output the data we got mid-transaction, but outside the CFThread. --->
<cfdump
var="#preThread#"
label="Pre CFThread"
/>
<!--- Output the data we got mid-transaction, inside the CFThread. --->
<cfdump
var="#cfthread#"
label="CFTHread"
/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment