So this might just be a fundamental failure in my understanding of the cftransaction
tag.
I have a bunch of Create Table SQL statements, one after the other, and in one of them there is an error, these are wrapped in a giant cftransaction
, however when the error occurs, it doesn't rollback the create statements.
<cfset variables.x = 0>
<cftry>
<cftransaction action="begin">
<cfquery name="variables.qTables" datasource="dev_db">
CREATE TABLE `dev_db`.`tbl_entries_import` (
`p_id` int( 11 ) default NULL ,
`p_Title` varchar( 255 ) default NULL ,
`p_FirstName` varchar( 255 ) default NULL ,
`p_unique_email` varchar( 255 ) default NULL ,
PRIMARY KEY ( `p_ID` ),
UNIQUE KEY `p_unique_email_UNIQUE` (`p_unique_email`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8;
</cfquery>
<cfset variables.x++>
<cftransaction action="setsavepoint" savepoint="#variables.x#"/>
<cfquery name="variables.qTables" datasource="dev_db">
CREATE TABLE `dev_db`.`tbl_entry2office_import` (
`entryID` int( 11 ) NOT NULL default '0',
`officeID` int( 11 ) NOT NULL default '0'
PRIMARY KEY ( `entryID` )
) ENGINE = InnoDB DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci;
</cfquery>
<cfset variables.x++>
<cftransaction action="setsavepoint" savepoint="#variables.x#"/>
</cftransaction>
<cfcatch>
<cfloop from="#variables.x#" to="1" index="variables.y" step="-1">
<cftransaction action="rollback" savepoint="#variables.y#">
</cfloop>
<cfdump var="#cfcatch#">
</cfcatch>
</cftry>
So after the second cfquery
(which should cause an error), I would expect the table from the first cfquery
to not exist in my schema, but currently it will do.
CREATE TABLE
statements cause an implicit commit, according to the docs: "13.3.3 Statements That Cause an Implicit Commit":
The statements listed in this section (and any synonyms for them) implicitly end any transaction active in the current session, as if you had done a COMMIT before executing the statement.
Data definition language (DDL) statements that define or modify database objects. ALTER DATABASE ... UPGRADE DATA DIRECTORY NAME, ALTER EVENT, ALTER PROCEDURE, ALTER SERVER, ALTER TABLE, CREATE DATABASE, CREATE EVENT, CREATE INDEX, CREATE PROCEDURE, CREATE SERVER, CREATE TABLE, DROP DATABASE, DROP EVENT, DROP INDEX, DROP PROCEDURE, DROP SERVER, DROP TABLE, RENAME TABLE, TRUNCATE TABLE.
(my emphasis)