@@ -1569,8 +1569,9 @@ _mongoc_client_retryable_write_command_with_stream (
1569
1569
retry_server_stream =
1570
1570
mongoc_cluster_stream_for_writes (& client -> cluster , & ignored_error );
1571
1571
1572
- if (retry_server_stream && retry_server_stream -> sd -> max_wire_version >=
1573
- WIRE_VERSION_RETRY_WRITES ) {
1572
+ if (retry_server_stream &&
1573
+ retry_server_stream -> sd -> max_wire_version >=
1574
+ WIRE_VERSION_RETRY_WRITES ) {
1574
1575
parts -> assembled .server_stream = retry_server_stream ;
1575
1576
bson_destroy (reply );
1576
1577
GOTO (retry );
@@ -1671,13 +1672,13 @@ mongoc_client_command_simple (mongoc_client_t *client,
1671
1672
*
1672
1673
* Execute a command on the server. If mode is MONGOC_CMD_READ or
1673
1674
* MONGOC_CMD_RW, then read concern is applied from @opts, or else from
1674
- * @default_rc, and read preferences are applied from @default_prefs.
1675
- * If mode is MONGOC_CMD_WRITE or MONGOC_CMD_RW, then write concern is
1676
- * applied from @opts if present, or else from @default_wc.
1675
+ * @default_rc, and read preferences are applied from @user_prefs, or else
1676
+ * from @default_prefs. If mode is MONGOC_CMD_WRITE or MONGOC_CMD_RW, then
1677
+ * write concern is applied from @opts if present, or else @default_wc.
1677
1678
*
1678
1679
* If mode is MONGOC_CMD_RAW, then read concern and write concern are
1679
1680
* applied from @opts only. Read preferences are applied from
1680
- * @read_prefs .
1681
+ * @user_prefs .
1681
1682
*
1682
1683
* The mongoc_client_t's read preference, read concern, and write concern
1683
1684
* are *NOT* applied.
@@ -1699,6 +1700,7 @@ _mongoc_client_command_with_opts (mongoc_client_t *client,
1699
1700
mongoc_command_mode_t mode ,
1700
1701
const bson_t * opts ,
1701
1702
mongoc_query_flags_t flags ,
1703
+ const mongoc_read_prefs_t * user_prefs ,
1702
1704
const mongoc_read_prefs_t * default_prefs ,
1703
1705
mongoc_read_concern_t * default_rc ,
1704
1706
mongoc_write_concern_t * default_wc ,
@@ -1708,6 +1710,7 @@ _mongoc_client_command_with_opts (mongoc_client_t *client,
1708
1710
mongoc_read_write_opts_t read_write_opts ;
1709
1711
mongoc_cmd_parts_t parts ;
1710
1712
const char * command_name ;
1713
+ const mongoc_read_prefs_t * prefs = COALESCE (user_prefs , default_prefs );
1711
1714
mongoc_server_stream_t * server_stream = NULL ;
1712
1715
mongoc_cluster_t * cluster ;
1713
1716
bson_t reply_local ;
@@ -1740,18 +1743,47 @@ _mongoc_client_command_with_opts (mongoc_client_t *client,
1740
1743
GOTO (err );
1741
1744
}
1742
1745
1746
+ if (_mongoc_client_session_in_txn (read_write_opts .client_session )) {
1747
+ if ((mode == MONGOC_CMD_READ || mode == MONGOC_CMD_RAW ) &&
1748
+ !IS_PREF_PRIMARY (user_prefs )) {
1749
+ bson_set_error (error ,
1750
+ MONGOC_ERROR_COMMAND ,
1751
+ MONGOC_ERROR_COMMAND_INVALID_ARG ,
1752
+ "Read preference in a transaction must be primary" );
1753
+ GOTO (err );
1754
+ }
1755
+
1756
+ if (!bson_empty (& read_write_opts .readConcern )) {
1757
+ bson_set_error (error ,
1758
+ MONGOC_ERROR_COMMAND ,
1759
+ MONGOC_ERROR_COMMAND_INVALID_ARG ,
1760
+ "Cannot set read concern after starting transaction" );
1761
+ GOTO (err );
1762
+ }
1763
+
1764
+ if (read_write_opts .writeConcern &&
1765
+ strcmp (command_name , "commitTransaction" ) != 0 &&
1766
+ strcmp (command_name , "abortTransaction" ) != 0 ) {
1767
+ bson_set_error (error ,
1768
+ MONGOC_ERROR_COMMAND ,
1769
+ MONGOC_ERROR_COMMAND_INVALID_ARG ,
1770
+ "Cannot set write concern after starting transaction" );
1771
+ GOTO (err );
1772
+ }
1773
+ }
1774
+
1743
1775
reply_ptr = reply ? reply : & reply_local ;
1744
1776
1745
1777
if (mode == MONGOC_CMD_READ || mode == MONGOC_CMD_RAW ) {
1746
1778
/* NULL read pref is ok */
1747
- if (!_mongoc_read_prefs_validate (default_prefs , error )) {
1779
+ if (!_mongoc_read_prefs_validate (prefs , error )) {
1748
1780
GOTO (err );
1749
1781
}
1750
1782
1751
- parts .read_prefs = default_prefs ;
1783
+ parts .read_prefs = prefs ;
1752
1784
} else {
1753
1785
/* this is a command that writes */
1754
- default_prefs = NULL ;
1786
+ prefs = NULL ;
1755
1787
}
1756
1788
1757
1789
cluster = & client -> cluster ;
@@ -1768,7 +1800,7 @@ _mongoc_client_command_with_opts (mongoc_client_t *client,
1768
1800
server_stream = mongoc_cluster_stream_for_writes (cluster , error );
1769
1801
} else {
1770
1802
server_stream = mongoc_cluster_stream_for_reads (
1771
- cluster , default_prefs , read_write_opts .client_session , error );
1803
+ cluster , prefs , read_write_opts .client_session , error );
1772
1804
}
1773
1805
1774
1806
if (!server_stream ) {
@@ -1856,18 +1888,18 @@ mongoc_client_read_command_with_opts (mongoc_client_t *client,
1856
1888
bson_t * reply ,
1857
1889
bson_error_t * error )
1858
1890
{
1859
- return _mongoc_client_command_with_opts (
1860
- client ,
1861
- db_name ,
1862
- command ,
1863
- MONGOC_CMD_READ ,
1864
- opts ,
1865
- MONGOC_QUERY_NONE ,
1866
- COALESCE ( read_prefs , client -> read_prefs ) ,
1867
- client -> read_concern ,
1868
- client -> write_concern ,
1869
- reply ,
1870
- error );
1891
+ return _mongoc_client_command_with_opts (client ,
1892
+ db_name ,
1893
+ command ,
1894
+ MONGOC_CMD_READ ,
1895
+ opts ,
1896
+ MONGOC_QUERY_NONE ,
1897
+ read_prefs ,
1898
+ client -> read_prefs ,
1899
+ client -> read_concern ,
1900
+ client -> write_concern ,
1901
+ reply ,
1902
+ error );
1871
1903
}
1872
1904
1873
1905
@@ -1885,6 +1917,7 @@ mongoc_client_write_command_with_opts (mongoc_client_t *client,
1885
1917
MONGOC_CMD_WRITE ,
1886
1918
opts ,
1887
1919
MONGOC_QUERY_NONE ,
1920
+ NULL ,
1888
1921
client -> read_prefs ,
1889
1922
client -> read_concern ,
1890
1923
client -> write_concern ,
@@ -1903,18 +1936,18 @@ mongoc_client_read_write_command_with_opts (
1903
1936
bson_t * reply ,
1904
1937
bson_error_t * error )
1905
1938
{
1906
- return _mongoc_client_command_with_opts (
1907
- client ,
1908
- db_name ,
1909
- command ,
1910
- MONGOC_CMD_RW ,
1911
- opts ,
1912
- MONGOC_QUERY_NONE ,
1913
- COALESCE ( read_prefs , client -> read_prefs ) ,
1914
- client -> read_concern ,
1915
- client -> write_concern ,
1916
- reply ,
1917
- error );
1939
+ return _mongoc_client_command_with_opts (client ,
1940
+ db_name ,
1941
+ command ,
1942
+ MONGOC_CMD_RW ,
1943
+ opts ,
1944
+ MONGOC_QUERY_NONE ,
1945
+ read_prefs ,
1946
+ client -> read_prefs ,
1947
+ client -> read_concern ,
1948
+ client -> write_concern ,
1949
+ reply ,
1950
+ error );
1918
1951
}
1919
1952
1920
1953
@@ -1934,6 +1967,7 @@ mongoc_client_command_with_opts (mongoc_client_t *client,
1934
1967
opts ,
1935
1968
MONGOC_QUERY_NONE ,
1936
1969
read_prefs ,
1970
+ NULL ,
1937
1971
client -> read_concern ,
1938
1972
client -> write_concern ,
1939
1973
reply ,
0 commit comments