Symfony 4 + ECCUBE, how to cycle queries between master slave nodes

Tram Ho

Preamble

As many of us know through the master slave node in RDS, by default famous PHP frameworks such as PHP, Symfony all support config master slave endpoint, for efficient query rotation, WRITE queries will be transferred. via the WRITE node, the READ (SELECT) queries will be passed to the READ node.

Problem.

And this is where the problem begins. ECCUBE is written based on symfony 4, but custom style:

All requests are wrapped in a transaction query of the DB query, even though the request is only a READ query

src/Eccube/EventListener/TransactionListener.php

And then the story goes further 🥲🥲🥲 , for doctrine2 (query library used in Symfony ECCUBE, an ORM library similar to Laravel’s Eloquent), queries written in a default transaction will be moved to the WRITE node ( because it considers this as an upcoming query). TOANG!!!!

Solution

Here we need to customize the connection class a bit, create a CustomMasterSlaveConnection.php file that inherits MasterSlaveConnection, the important point here is the connect() method. They are overriden the connect method to select the appropriate WRITE READ node, implement an additional flag to lock, in this case we can force the query to run on any NODE.

Usage is quite easy, just lock() at the beginning of the segment you want to use the READ node, then use the unlock() function after that.

Conclude

Hopefully the above is a solution to help you customize the ECCUBE framework to choose the right READ WRITE node to meet the system’s load capacity.

Tks for reading!!!

Share the news now

Source : Viblo