Skip to content

Model Class

invokeWithTransaction()

invokeWithTransaction() — returns any

Available in: model Category: Miscellaneous Functions

Runs a specified model method inside a single database transaction. This ensures that all database operations within the method are treated as a single atomic unit: either all succeed or all fail.

NameTypeRequiredDefaultDescription
methodstringyesModel method to run.
transactionstringnocommitSet this to commit to update the database, rollback to run all the database queries but not commit them, or none to skip transaction handling altogether.
isolationstringnoread_committedIsolation level to be passed through to the cftransaction tag. See your CFML engine’s documentation for more details about cftransaction’s isolation attribute.
1. This is the method to be run inside a transaction.
public boolean function transferFunds(required any personFrom, required any personTo, required numeric amount) {
	if (arguments.personFrom.withdraw(arguments.amount) && arguments.personTo.deposit(arguments.amount)) {
		return true;
	} else {
		return false;
	}
}

local.david = model("Person").findOneByName("David");
local.mary = model("Person").findOneByName("Mary");
invokeWithTransaction(method="transferFunds", personFrom=local.david, personTo=local.mary, amount=100);