Reputation: 69
I need to use transactions in laravel 8 and I'm using the Jenssegers-laravel-mongodb package. The beginTransaction
method on the DB facade doesn't work for mongoDB, so I tried the following method:
$session = DB::connection('mongodb')->getMongoClient()->startSession();
$session->startTransaction();
try {
$task = Task::create($request->only('reminder', 'priority'));
$activity = $task->activity()->create($request->only('topic', 'description', 'creator_id'));
$session->commitTransaction();
} catch (\Exception $exception) {
$session->abortTransaction();
}
When a query fails in the try
block, it throws the exception but does not roll back the transactions. I also found a similar question here: Laravel mongodb transactions does not rollback. I tried the given solution in the single answer, but it returns the following error:
{message: "Unsupported driver [].", exception: "InvalidArgumentException"}
Upvotes: 3
Views: 605