PageRenderTime 38ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/wheels/model/transactions.cfm

http://cfwheels.googlecode.com/
ColdFusion | 90 lines | 75 code | 10 blank | 5 comment | 1 complexity | 177b167c82a6e3a2a8aefba40ed56bbf MD5 | raw file
Possible License(s): Apache-2.0, CPL-1.0
  1. <cffunction name="invokeWithTransaction" returntype="any" access="public" output="false" hint="Runs the specified method within a single database transaction."
  2. examples=
  3. '
  4. <!--- This is the method to be run inside a transaction --->
  5. <cffunction name="tranferFunds" returntype="boolean" output="false">
  6. <cfargument name="personFrom">
  7. <cfargument name="personTo">
  8. <cfargument name="amount">
  9. <cfif arguments.personFrom.withdraw(arguments.amount) and arguments.personTo.deposit(arguments.amount)>
  10. <cfreturn true>
  11. <cfelse>
  12. <cfreturn false>
  13. </cfif>
  14. </cffunction>
  15. <cfset david = model("Person").findOneByName("David")>
  16. <cfset mary = model("Person").findOneByName("Mary")>
  17. <cfset invokeWithTransaction(method="transferFunds", personFrom=david, personTo=mary, amount=100)>
  18. '
  19. categories="model-class" chapters="transactions" functions="new,create,save,update,updateByKey,updateOne,updateAll,delete,deleteByKey,deleteOne,deleteAll">
  20. <cfargument name="method" type="string" required="true" hint="Model method to run.">
  21. <cfargument name="transaction" type="string" default="commit" hint="See documentation for @save.">
  22. <cfargument name="isolation" type="string" default="read_committed" hint="Isolation level to be passed through to the `cftransaction` tag. See your CFML engine's documentation for more details about `cftransaction`'s `isolation` attribute.">
  23. <cfset var loc = {} />
  24. <cfset loc.methodArgs = $setProperties(properties=StructNew(), argumentCollection=arguments, filterList="method,transaction,isolation", setOnModel=false, $useFilterLists=false)>
  25. <cfset loc.connectionArgs = this.$hashedConnectionArgs()>
  26. <cfset loc.closeTransaction = true>
  27. <cfif not StructKeyExists(variables, arguments.method)>
  28. <cfset $throw(type="Wheels", message="Model method not found!", extendedInfo="The method `#arguments.method#` does not exist in this model.")>
  29. </cfif>
  30. <!--- create the marker for an open transaction if it doesn't already exist --->
  31. <cfif not StructKeyExists(request.wheels.transactions, loc.connectionArgs)>
  32. <cfset request.wheels.transactions[loc.connectionArgs] = false>
  33. </cfif>
  34. <!--- if a transaction is already marked as open, change the mode to 'none', otherwise open one --->
  35. <cfif request.wheels.transactions[loc.connectionArgs]>
  36. <cfset arguments.transaction = "none">
  37. <cfset loc.closeTransaction = false>
  38. <cfelse>
  39. <cfset request.wheels.transactions[loc.connectionArgs] = true>
  40. </cfif>
  41. <!--- run the method --->
  42. <cfswitch expression="#arguments.transaction#">
  43. <cfcase value="commit,rollback">
  44. <cftransaction action="begin" isolation="#arguments.isolation#">
  45. <cftry>
  46. <cfset loc.returnValue = $invoke(method=arguments.method, componentReference=this, invokeArgs=loc.methodArgs)>
  47. <cfif IsBoolean(loc.returnValue) and loc.returnValue>
  48. <cftransaction action="#arguments.transaction#" />
  49. <cfelse>
  50. <cftransaction action="rollback" />
  51. </cfif>
  52. <cfcatch type="any">
  53. <cftransaction action="rollback" />
  54. <cfset request.wheels.transactions[loc.connectionArgs] = false>
  55. <cfrethrow />
  56. </cfcatch>
  57. </cftry>
  58. </cftransaction>
  59. </cfcase>
  60. <cfcase value="none">
  61. <cfset loc.returnValue = $invoke(method=arguments.method, componentReference=this, invokeArgs=loc.methodArgs)>
  62. </cfcase>
  63. <cfdefaultcase>
  64. <cfset $throw(type="Wheels", message="Invalid transaction type", extendedInfo="The transaction type of `#arguments.transaction#` is invalid. Please use `commit`, `rollback` or `none`.")>
  65. </cfdefaultcase>
  66. </cfswitch>
  67. <!--- close the connection --->
  68. <cfif loc.closeTransaction>
  69. <cfset request.wheels.transactions[loc.connectionArgs] = false>
  70. </cfif>
  71. <!--- check the return type --->
  72. <cfif not IsBoolean(loc.returnValue)>
  73. <cfset $throw(type="Wheels", message="Invalid return type", extendedInfo="Methods invoked using `invokeWithTransaction` must return a boolean value.")>
  74. </cfif>
  75. <cfreturn loc.returnValue />
  76. </cffunction>
  77. <cffunction name="$hashedConnectionArgs" returntype="string" access="public" output="false">
  78. <cfreturn Hash(variables.wheels.class.connection.datasource & variables.wheels.class.connection.username & variables.wheels.class.connection.password)>
  79. </cffunction>