Concurrency caused session problems, and no solution was found

php version:7.4
laravel veresion:7.0
mongodb version:4.2.1
QPS:100~200
Error:
Exception: MongoDB\Driver\Exception\BulkWriteException(code:225): Cannot start transaction 40 on session 86f7dbe4-3142-40c3-9618-411755a9cb67 - abWnSN9iDr/ue5yq5STQ36kflt2oNBjuZhxOcaTuAVg= because a newer transaction 48 has already started. at /data/www/questions/vendor/mongodb/mongodb/src/Operation/InsertOne.php:134
Exception Trace:#0 /data/www/questions/vendor/mongodb/mongodb/src/Operation/InsertOne.php(134): MongoDB\Driver\Server->executeBulkWrite()
#1 /data/www/questions/vendor/mongodb/mongodb/src/Collection.php(931): MongoDB\Operation\InsertOne->execute()
#2 [internal function]: MongoDB\Collection->insertOne()
#3 /data/www/questions/vendor/jenssegers/mongodb/src/Jenssegers/Mongodb/Collection.php(42): call_user_func_array()
#4 /data/www/questions/vendor/jenssegers/mongodb/src/Jenssegers/Mongodb/Query/Builder.php(580): Jenssegers\Mongodb\Collection->__call()
#5 /data/www/questions/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(1422): Jenssegers\Mongodb\Query\Builder->insertGetId()
#6 /data/www/questions/vendor/jenssegers/mongodb/src/Jenssegers/Mongodb/Eloquent/Builder.php(78): Illuminate\Database\Eloquent\Builder->__call()
#7 /data/www/questions/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(902): Jenssegers\Mongodb\Eloquent\Builder->insertGetId()
#8 /data/www/questions/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(867): Illuminate\Database\Eloquent\Model->insertAndSetId()
#9 /data/www/questions/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(730): Illuminate\Database\Eloquent\Model->performInsert()
#10 /data/www/questions/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(776): Illuminate\Database\Eloquent\Model->save()
#11 /data/www/questions/vendor/laravel/framework/src/Illuminate/Support/helpers.php(433): Illuminate\Database\Eloquent\Builder->Illuminate\Database\Eloquent{closure}()
#12 /data/www/questions/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(777): tap()
#13 /data/www/questions/app/Http/Controllers/Api/Question/V153/QuestionsController.php(757): Illuminate\Database\Eloquent\Builder->create()

Hi,

from the trace I can only see that you’re creating a new object through the query builder, but nothing else that would indicate that transactions are in use. Since you are using Laravel 7 I assume you’re also using an older version of the jenssegers/laravel-mongodb package, which doesn’t explicitly support transactions. When did you first observe the error? Did it start appearing as concurrency increased (i.e. QPS started getting higher), was it after a version update of packages, or has it always been there?

To better narrow down the issue, please provide the following information:

  • Version of mongodb/mongodb, which you can find out by running composer show mongodb/mongodb
  • Version of ext-mongodb, which you can find out by running php --ri mongodb
  • Version of jenssegers/laravel-mongodb, which you can find out by running composer show laravel/mongodb
  • Information about the MongoDB deployment, e.g. standalone, replica set, sharded cluster

Depending on driver version, the retryWrites connection option may help in finding out the cause. This defaults to true on newer driver versions, so you could test explicitly setting it to false in your connection string to see if the error persists. Note that this is not a solution to fix the problem, but rather a way for us to find out what’s going on. The feature retries certain write operations once in case of specific errors to reduce the amount of write errors you receive. More information on this feature can be found in the documentation.

Thanks,
Andreas

name : mongodb/mongodb
descrip. : MongoDB driver library
keywords : database, driver, mongodb, persistence
versions : * 1.8.0

mongodb
MongoDB support => enabled
MongoDB extension version => 1.9.0
MongoDB extension stability => stable
libbson bundled version => 1.17.2
libmongoc bundled version => 1.17.2

Not used composer show laravel/mongodb
name : jenssegers/mongodb
descrip. : A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)
keywords : database, eloquent, laravel, model, moloquent, mongo, mongodb
versions : * v3.7.2

Stand-alone 3-node

I have no way to test the recurrence of online problems. Pressure test 200 did not recur.

Thanks,
Yong

I have been troubled for a long time, the project test, the transaction is not effective, I don’t understand why the session is generated?

Thanks,
Yong