Skip to content

Error: Cannot set read preferences after starting transaction #562

@sandrokeil

Description

@sandrokeil

I don't explicitly set a read preference but it looks that this library sets always read preference primary for each query if not specified. This results in the error Cannot set read preferences after starting transaction.

Here is an example which fails:

$client = new Client();
$collection = $client->selectCollection('mydb', 'test');
$session = $client->startSession();
$session->startTransaction(
    [
        'readConcern' => new \MongoDB\Driver\ReadConcern('snapshot'),
        'writeConcern' => new \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY),
    ]
);

$options = [
    'session' => $session
];

$collection->findOne([], $options); // -> Exception

How can I read data from a collection inside a transaction?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions