Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: what happens if I have separate read/write connections configured, and do both reads and writes within a transaction?

If I have reads/writes configured in database.php, and I start a transaction like DB::transaction(function() { // series of reads and writes }), will everything run on the write connection, or will there be two separate transactions, or will it just explode?

like image 582
timetofly Avatar asked Oct 22 '25 15:10

timetofly


2 Answers

https://github.com/laravel/framework/pull/3272

getReadPdo() now checks if the query is within a transaction, and in that case returns the same PDO connection as the write.

like image 74
Steven M Avatar answered Oct 25 '25 05:10

Steven M


If you're saying you want to do both reads and writes within a transaction using the same connection, then that will work fine - you can read from the database during the transaction.

If you're saying you want to read using one connection while you're writing as part of a transaction on the other connection, that will work fine too although you might have times when the read connection will block until the write connection transaction is finished.

like image 36
Kryten Avatar answered Oct 25 '25 04:10

Kryten