|
| 1 | +--TEST-- |
| 2 | +MongoDB\Driver\Session::startTransaction() Transient Error Test |
| 3 | +--SKIPIF-- |
| 4 | +<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?> |
| 5 | +<?php skip_if_not_clean(DATABASE_NAME, COLLECTION_NAME); ?> |
| 6 | +<?php skip_if_not_replica_set(); ?> |
| 7 | +<?php skip_if_server_version('<', '4.0'); ?> |
| 8 | +--FILE-- |
| 9 | +<?php |
| 10 | +require_once __DIR__ . "/../utils/basic.inc"; |
| 11 | + |
| 12 | +$manager = new MongoDB\Driver\Manager(URI); |
| 13 | + |
| 14 | +/* Create collections as that can't be (automatically) done in a transaction */ |
| 15 | +$cmd = new \MongoDB\Driver\Command([ |
| 16 | + 'create' => COLLECTION_NAME, |
| 17 | +]); |
| 18 | +$manager->executeCommand(DATABASE_NAME, $cmd); |
| 19 | + |
| 20 | +/* Insert Data */ |
| 21 | +$bw = new \MongoDB\Driver\BulkWrite(); |
| 22 | +$bw->insert( [ '_id' => 0, 'msg' => 'Initial Value' ] ); |
| 23 | +$manager->executeBulkWrite(NS, $bw); |
| 24 | + |
| 25 | +/* First 'thread', try to update document, but don't close transaction */ |
| 26 | +$sessionA = $manager->startSession(); |
| 27 | +$sessionA->startTransaction( [ |
| 28 | + 'readConcern' => new \MongoDB\Driver\ReadConcern( "snapshot" ), |
| 29 | + 'writeConcern' => new \MongoDB\Driver\WriteConcern( \MongoDB\Driver\WriteConcern::MAJORITY ) |
| 30 | +] ); |
| 31 | + |
| 32 | +$bw = new \MongoDB\Driver\BulkWrite(); |
| 33 | +$bw->update( [ '_id' => 0 ], [ '$set' => [ 'msg' => 'Update from session A' ] ] ); |
| 34 | +$manager->executeBulkWrite(NS, $bw, ['session' => $sessionA]); |
| 35 | + |
| 36 | + |
| 37 | +/* Second 'thread', try to update the same document, should trigger exception. In handler, commit |
| 38 | + * first settion, verify result, and redo this transaction. */ |
| 39 | +$sessionB = $manager->startSession(); |
| 40 | +$sessionB->startTransaction( [ |
| 41 | + 'readConcern' => new \MongoDB\Driver\ReadConcern( "snapshot" ), |
| 42 | + 'writeConcern' => new \MongoDB\Driver\WriteConcern( \MongoDB\Driver\WriteConcern::MAJORITY ) |
| 43 | +] ); |
| 44 | + |
| 45 | +try { |
| 46 | + $bw = new \MongoDB\Driver\BulkWrite(); |
| 47 | + $bw->update( [ '_id' => 0 ], [ '$set' => [ 'msg' => 'Update from session B' ] ] ); |
| 48 | + $manager->executeBulkWrite(NS, $bw, ['session' => $sessionB]); |
| 49 | +} catch (MongoDB\Driver\Exception\BulkWriteException $e) { |
| 50 | + echo $e->hasErrorLabel('TransientTransactionError') ? |
| 51 | + "found a TransientTransactionError" : "did NOT get a TransientTransactionError", "\n"; |
| 52 | +} |
| 53 | +?> |
| 54 | +===DONE=== |
| 55 | +<?php exit(0); ?> |
| 56 | +--EXPECTF-- |
| 57 | +found a TransientTransactionError |
| 58 | +===DONE=== |
0 commit comments