Skip to content

Commit ce2e43e

Browse files
committed
PHPC-1359: Prohibit startTransaction() on sharded clusters
In versions of the driver that do not support mongos pinning, we should prohibit starting a transaction on a sharded cluster (even if the server version might actually support transactions).
1 parent 2198e55 commit ce2e43e

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/MongoDB/Session.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ zend_class_entry* php_phongo_session_ce;
3535
return; \
3636
}
3737

38+
static bool php_phongo_topology_is_sharded_cluster(mongoc_client_t* client)
39+
{
40+
mongoc_server_description_t* sd = mongoc_client_select_server(client, true, NULL, NULL);
41+
42+
return (sd && !strcmp(mongoc_server_description_type(sd), php_phongo_server_description_type_map[PHONGO_SERVER_MONGOS].name));
43+
}
44+
3845
static bool php_phongo_session_get_timestamp_parts(zval* obj, uint32_t* timestamp, uint32_t* increment TSRMLS_DC)
3946
{
4047
bool retval = false;
@@ -334,6 +341,11 @@ static PHP_METHOD(Session, startTransaction)
334341
return;
335342
}
336343

344+
if (php_phongo_topology_is_sharded_cluster(mongoc_client_session_get_client(intern->client_session))) {
345+
phongo_throw_exception(PHONGO_ERROR_RUNTIME TSRMLS_CC, "PHP MongoDB driver %s does not support running multi-document transactions on sharded clusters", PHP_MONGODB_VERSION);
346+
return;
347+
}
348+
337349
if (!mongoc_client_session_start_transaction(intern->client_session, txn_options, &error)) {
338350
phongo_throw_exception_from_bson_error_t(&error TSRMLS_CC);
339351
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
MongoDB\Driver\Session::startTransaction() does not support sharded clusters
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php skip_if_not_libmongoc_crypto() ?>
6+
<?php skip_if_not_mongos(); ?>
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+
$session = $manager->startSession();
14+
15+
echo throws(function() use ($session) {
16+
$session->startTransaction();
17+
}, 'MongoDB\Driver\Exception\RuntimeException'), "\n";
18+
19+
?>
20+
===DONE===
21+
<?php exit(0); ?>
22+
--EXPECTF--
23+
OK: Got MongoDB\Driver\Exception\RuntimeException
24+
PHP MongoDB driver %s does not support running multi-document transactions on sharded clusters
25+
===DONE===

0 commit comments

Comments
 (0)