Coder Social home page Coder Social logo

Comments (5)

VeryStrongFingers avatar VeryStrongFingers commented on May 31, 2024

Logistically it would make more sense to override the Model methods 'create'/'save' to temporarily change the read connection service for these methods.

Altering the code base to account for anyone running a master-slave database infrastructure by 'reading' from the 'write' DB service doesn't make the most sense out of all possible solutions, in my opinion.

Based on the code at https://github.com/phalcon/cphalcon/blob/c8d4916bbfd0cb18395fe25d8c5a663fc5077d8f/phalcon/mvc/model.zep#L2959
You could do something like

namespace Custom;

class Model extends \Phalcon\Mvc\Model {
    public function create($data = NULL, $whiteList = NULL)
    {
        $originalReadService = $this->getReadConnectionService();
        $this->setReadConnectionService($this->getWriteConnectionService());
        $result = parent::create($data, $whiteList);
        $this->setReadConnectionService($originalReadService);
        return $result;
    }
}

from phalcon.

dschissler avatar dschissler commented on May 31, 2024

@sergeyklay Can we add this to the 4.0 milestone? We can evaluate it later for its validity. It seems though that it could eliminate some rare gnarly impossible to reproduce race conditions.

from phalcon.

guweigang avatar guweigang commented on May 31, 2024

Maybe we should not check if record exists, it's not just a master-slave latency problem. Even using the same connection here, unless you use select...for update under a transaction,or else you can not avoid this 'cause of concurrency. @ifsnow @Mechzeit

Sent from my OnePlus ONEPLUS A5000 using FastHub

from phalcon.

acmenna avatar acmenna commented on May 31, 2024

Master-slave setup does not work with transactions either. If a transaction is active, all reads should be kept in master regardless the read connection. Doctrine has some cool rules to handle all these scenarios mentioned in this issue. I marked in bold the most important one that will covered the latency and the transaction issue.

http://www.doctrine-project.org/api/dbal/2.0/class-Doctrine.DBAL.Connections.MasterSlaveConnection.html

Slave if master was never picked before and ONLY if 'getWrappedConnection' or 'executeQuery' is used.
Master picked when 'exec', 'executeUpdate', 'insert', 'delete', 'update', 'createSavepoint', 'releaseSavepoint', 'beginTransaction', 'rollback', 'commit', 'query' or 'prepare' is called.
If master was picked once during the lifetime of the connection it will always get picked afterwards.
One slave connection is randomly picked ONCE during a request.

from phalcon.

guweigang avatar guweigang commented on May 31, 2024

@acmenna Agree. But I don't think this could help exists() before doLowUpdate.

from phalcon.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.