Skip to content

Model Class

invokeWithTransaction()

invokeWithTransaction() — returns any

Available in: model Category: Miscellaneous Functions

Runs the specified method within a single database transaction.

NameTypeRequiredDefaultDescription
methodstringyes—Model 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.
isolationstringno—Isolation level to be passed through to the cftransaction tag. Defaults to the transactionIsolation setting (read_committed unless the app changes it). An empty string sends no isolation attribute, so the transaction uses the engine’s or driver’s default. See your CFML engine’s documentation for more details about cftransaction’s isolation attribute.
// 1. Run a custom model method inside a database transaction.
// Define the method on the model (e.g. Person.cfc):
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;
	}
}

// Then invoke it wrapped in a transaction from a controller action:
local.david = model("Person").findOneByName("David");
local.mary = model("Person").findOneByName("Mary");
local.success = model("Person").invokeWithTransaction(method="transferFunds", personFrom=local.david, personTo=local.mary, amount=100);

// 2. Run in rollback mode to test queries without committing changes.
local.success = model("Person").invokeWithTransaction(method="transferFunds", personFrom=local.david, personTo=local.mary, amount=100, transaction="rollback");

// 3. Run with a stricter isolation level (e.g. serializable) to prevent phantom reads.
local.success = model("Person").invokeWithTransaction(method="transferFunds", personFrom=local.david, personTo=local.mary, amount=100, isolation="serializable");