Skip to main content
Participant
June 19, 2009
Question

About CFTransaction

  • June 19, 2009
  • 2 replies
  • 624 views
The classic example of using rollback and commit in this scenario is in an online banking system 
that allows a user to transfer funds from one account to another. An obvious business rule in this
scenario is that a user cannot withdraw or transfer more money from an account than that account
contains. The code for transaction-based logic looks something like the following:

http://www.adobe.com/devnet/coldfusion/articles/cftransaction.html

<cftransaction> <cfquery name="qUpdAccount1" datasource="myDSN">   UPDATE Accounts SET accountBalance = accountBalance - #withdrawAmount# WHERE accountid = #accountID1# </cfquery> <cfquery name="qUpdAccount2" datasource="myDSN">   UPDATE Accounts SET accountBalance = accountBalance + #withdrawAmount# WHERE accountid = #accountID2# </cfquery> <cfquery name="qAccount1Balance" datasource="myDSN">   SELECT accountBalance FROM accounts WHERE accountid = #accountID1# </cfquery> <cfif qAccount1Balance.accountBalance lt 0>   <cftransaction action="ROLLBACK" /> <cfelse>   <cftransaction action="COMMIT" /> </cfif> </cftransaction>

I was tried above example about cftransaction but it is not working.Please provide any solution for this.
    This topic has been closed for replies.

    2 replies

    Dileep_NR
    Inspiring
    June 19, 2009
    Please try this 

    <cftransaction>

    <cfquery name="qAccount1Balance" datasource="myDSN">
      SELECT accountBalance FROM accounts WHERE accountid = #accountID1#
    </cfquery>


    <cfquery name="qUpdAccount1" datasource="myDSN">
      UPDATE Accounts SET accountBalance
    = accountBalance - #withdrawAmount# WHERE accountid = #accountID1#
    </cfquery>
    <cfquery name="qUpdAccount2" datasource="myDSN">
      UPDATE Accounts SET accountBalance
    = accountBalance + #withdrawAmount# WHERE accountid = #accountID2#
    </cfquery>

    <cfif qAccount1Balance.accountBalance lt withdrawAmount>
      <cftransaction action="ROLLBACK" />
    <cfelse>
      <cftransaction action="COMMIT" />
    </cfif>
    </cftransaction>
    Participating Frequently
    June 19, 2009

    We need additional information about the database that you are using.

    Not all databases are created equal and there are some that don't

    support transactions (CF transaction support is a wrapper around the

    native transaction support in your database - if your database doesn't

    support transactions then neither does CF). For example for MySQL only

    the INNODB engine has transactions.

    Mack