Automatically connect to MySQL master if slave not working in Symfony2

Viewed 1262

I've set up a master-slave connection that works just fine. It seems to read from the slave and write to the master. For the MySQL part I used this guide. Any modifications done to the master are immediately applied to the slave as well. Both databases use the 5.6 version.

doctrine:
    dbal:
        default_connection:   default
        connections:
            default:
                driver:   pdo_mysql
                host:     "%database_host%"
                port:     "%database_port%"
                dbname:   "%database_name%"
                user:     "%database_user%"
                password: "%database_password%"
                charset:  UTF8

                slaves:
                    slave1:
                        host:     "%sdatabase_host%"
                        port:     "%sdatabase_port%"
                        dbname:   "%sdatabase_name%"
                        user:     "%sdatabase_user%"
                        password: "%sdatabase_password%"

However, if the slave's mysql server is down (or badly configured), wouldn't it make sense for the connection to be made to the master? In this case, the whole app crashes and I find that a bit annoying.

I'd like to know if there is a way to connect to the master if the slave is not working. Or please tell me if it's a bad practice.

I believe it has to do with MasterSlaveConnection since that one seems to be used when slaves are configured for a dbal connection, otherwise Connection is used.

1 Answers
Related