Skip to content

Commit 36f2577

Browse files
committed
Merged pull request #880
2 parents 3d5a0d2 + c5f82b7 commit 36f2577

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

php_phongo.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,9 +764,10 @@ bool phongo_execute_bulk_write(mongoc_client_t* client, const char* namespace, p
764764
if (!success) {
765765
if (error.domain == MONGOC_ERROR_SERVER || error.domain == MONGOC_ERROR_WRITE_CONCERN) {
766766
zend_throw_exception(php_phongo_bulkwriteexception_ce, error.message, error.code TSRMLS_CC);
767+
phongo_exception_add_error_labels(&reply TSRMLS_CC);
767768
phongo_add_exception_prop(ZEND_STRL("writeResult"), return_value TSRMLS_CC);
768769
} else {
769-
phongo_throw_exception_from_bson_error_t(&error TSRMLS_CC);
770+
phongo_throw_exception_from_bson_error_and_reply_t(&error, &reply TSRMLS_CC);
770771
}
771772
}
772773

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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

Comments
 (0)