ColdFusion 9.0 Resources |
cftransactionDescriptionFor enterprise database management systems that support transaction processing, instructs the database management system to treat multiple database operations as a single transaction. Provides database commit and rollback processing. See the documentation for your database management system to determine whether it supports SQL transaction processing. Syntax<cftransaction action = "begin|commit|rollback|setsavepoint" isolation = "read_uncommitted|read_committed|repeatable_read" savepoint = "savepoint name"> </cftransaction> Note: You can specify
this tag’s attributes in an attributeCollection attribute
whose value is a structure. Specify the structure name in the attributeCollection attribute
and use the tag’s attribute names as structure keys.
See alsocfinsert, cfprocparam, cfprocresult, cfquery, cfqueryparam, cfstoredproc, cfupdate; Commits, rollbacks, and transactions and Tags as functions and operators in Developing ColdFusion Applications HistoryColdFusion 8: Added the setsavepoint value to the action attribute. Added the savepoint attribute. Attributes
UsageIf you do not specify a value for the action attribute, automatic transaction processing proceeds as follows:
Example<p>The cftransaction tag can be used to group multiple queries that use the cfquery tag into one business event. 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. <p>This a view-only example. <!--- <cftransaction> <cfquery name='makeNewCourse' datasource='Snippets'> INSERT INTO Courses (Number, Descript) VALUES ('#myNumber#', '#myDescription#') </cfquery> <cfquery name='insertNewCourseToList' datasource='Snippets'> INSERT INTO CourseList (CorNumber, CorDesc, Dept_ID, CorName, CorLevel, LastUpdate) VALUES ('#myNumber#', '#myDescription#', '#myDepartment#', '#myDescription#', '#myCorLevel#', #Now()#) </cfquery> </cftransaction> ---> You can set savepoints at the completion of insert, update, and delete actions of a transaction. You then use error handling logic to determine whether it is necessary to roll back to a previous savepoint. Example<!--- This example performs batch processing of withdrawals ---> <!--- from a bank account. The withdrawal amounts are stored ---> <!--- in an array. ---> <!--- There is a CFC named bank.cfc whose contains appear ---> <!--- after the example. ---> <cftransaction> <!--- Get the account balance. ---> <cfinvoke component="bank" method="getBalance" returnvariable="getacctbalance" accountnum=1> <cfloop index="withdrawnum" from="1" to="#ArrayLen(withdrawals)#"> <!--- Set a savepoint before making the withdrawal. ---> <cfset noxfer = "point" & #withdrawnum#> <cftransaction action = "setsavepoint" savepoint = "#noxfer#"/> <!--- Make the withdrawal. ---> <cfinvoke component="bank" method="makewithdrawal" returnvariable="getacctbalance" accountnum=1 withdrawamount="#withdrawals[withdrawnum]#"> <!--- Get the account balance. ---> <cfinvoke component="bank" method="getBalance" returnvariable="getacctbalance" accountnum=1> <!--- If the balance is negative, roll back the transaction. ---> <cfif getacctbalance.balance lt 0> <cftransaction action="rollback" savepoint="#noxfer#" /> </cfif> </cfloop> </cftransaction> <!--- The bank.cfc contains the following: cfcomponent> <cffunction name="getBalance" access="public" returntype="query"> <cfargument name="accountnum" type="numeric" required="yes"> <cfquery name="getacctbalance" datasource="testsqlserver"> SELECT * FROM dbo.mybank WHERE accountid = #accountnum# </cfquery> <cfreturn getacctbalance> </cffunction> <cffunction name="makewithdrawal" access="public" returntype="query"> <cfargument name="accountnum" type="numeric" required="yes"> <cfargument name="withdrawamount" type="numeric" required="yes"> <cfquery name="withdrawfromacct" datasource="testsqlserver"> UPDATE dbo.mybank SET balance = balance - #withdrawamount# WHERE accountid = 1 </cfquery> <cfinvoke method="getBalance" returnvariable="getacctbalance" accountnum=1> <cfreturn getacctbalance> </cffunction> </cfcomponent> ---> |