i'm trying to learn mongodb transactions using php-mongodb library v1.5 but i've found some problemes.
i've tried to start, commit, and abort transaction using the giving methods but abortTransaction is not working for me :
$session = self::$instance->startSession();
$this->db = self::$instance->{"mydb"};
$session->startTransaction();
$this->db->users->deleteOne([
'_id' => new MongoDB\BSON\ObjectId('5c88e197df815495df201a38')
]);
$session->abortTransaction();
$session->endSession();
the transaction is always commited even after the abort action !!!
what i'm missing here please save my day :(
the transaction is always commited even after the abort action
This is because the delete operation doesn't utilise the session
object that you have instantiated. You need to pass the session
as a $options
parameter MongoDB\Collection::deleteOne(). Otherwise it will execute outside of the transaction. For example:
$session->startTransaction();
$this->db->users->deleteOne(
['_id' => new MongoDB\BSON\ObjectId('5c88e197df815495df201a38')],
['session' => $session]
);
See also MongoDB Transactions for more information