[13.x] Add withoutAffectingStickiness to Connection

Merged
JA
7d ago
+28-0

With sticky enabled, any write makes all later reads go to the write connection.

So a job that records when it started reads from the write connection for the rest of the job:

$this->task->update(['started_at' => now()]);
$this->task->run(); // Every read within the task is now stuck on the write connection, even if the task never writes...

This PR adds a withoutRecordingModifications method, so we can skip over it being sticky where we dont need it to be:

DB::withoutAffectingStickiness(function () {
    $this->task->update(['started_at' => now()]);
});
$this->task->run(); // Reads are now on the read connection, until the task writes something itself...

If a write happened before the call, reads stay on the write connection as usual.

Today this means saving and restoring the state by hand, so thought I'd try this PR..

I wasn't sold on the method name, but can't think of anything elite

Thanks