From e8d69184de2656ddb7f34f79105d3c91fe21b25e Mon Sep 17 00:00:00 2001 From: prolic Date: Tue, 28 Jun 2016 03:54:45 +0800 Subject: [PATCH] better concurrency handling --- README.md | 2 +- src/MongoDbEventStoreAdapter.php | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c21473d..b0df062 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Write concern This adapter uses a transaction timeout of 50 secs by default. -The default write concern for this adapater is acknowledged and journaled (['w' => 1, 'j' => true]). +The default write concern for this adapater is acknowledged (['w' => 1]). It's possible to change both values by injecting them into the constructor or by using the factory. diff --git a/src/MongoDbEventStoreAdapter.php b/src/MongoDbEventStoreAdapter.php index 0138925..0623019 100644 --- a/src/MongoDbEventStoreAdapter.php +++ b/src/MongoDbEventStoreAdapter.php @@ -73,7 +73,6 @@ final class MongoDbEventStoreAdapter implements Adapter, CanHandleTransaction */ private $writeConcern = [ 'w' => 1, - 'j' => true ]; /** @@ -174,8 +173,13 @@ public function appendTo(StreamName $streamName, Iterator $streamEvents) try { $insertBatch->execute(); + } catch (\MongoDuplicateKeyException $e) { + throw new ConcurrencyException('At least one event with same version exists already', 0, $e); } catch (\MongoWriteConcernException $e) { - throw new ConcurrencyException('', 0, $e); + $code = $e->getDocument()['writeErrors'][0]['code']; + if (in_array($code, [11000, 11001, 12582])) { + throw new ConcurrencyException('At least one event with same version exists already', 0, $e); + } } }