0

I found a module someone created and looked into the code. I found that this person was making a lot connection as such in a single block:

public function checkStoreExists($storeID){
    $connection = $this->_resource->getConnection(\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION);
    // some code
}
public function checkCategoryExists($categoryID){
    $connection = $this->_resource->getConnection(\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION);
    // some code
}
public function checkBrandExists($brandID){
    $connection = $this->_resource->getConnection(\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION);
    // some code
}
public function checkRangeExists($rangeID){
    $connection = $this->_resource->getConnection(\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION);
    // some code
}

public function checkProductExists($productID, $rangeID){
    $connection = $this->_resource->getConnection(\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION);
    // some code
}

public function checkProductOptionExists($productID, $rangeID, $productSku){
    $connection = $this->_resource->getConnection(\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION);
    // some code
}

public function GetNextProductIndex(){
    $connection = $this->_resource->getConnection(\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION);
    // some code
}

public function GetNextProductOptionIndex(){
    $connection = $this->_resource->getConnection(\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION);
    // some code
}

Will that have any impact on performance? Is this okey to leave or should I adjust this somehow?

0

You can modify following way:


private $connection;

/**
 * @return mixed
 */
public function getConnection()
{
    if (!$this->connection) {
        $this->connection = $this->_resource->getConnection(\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION);
    }

    return $this->connection;
}

now use following way:


$connection = $this->getConnection()
  • Great! Thanks. The way he done it above would have any impact on performane? Or any other down effects? Curiosity – Bare Feet Jan 11 at 17:52
  • Open vendor/magento/framework/App/ResourceConnection.php and check getConnection method then you understand the effect. – Sohel Rana Jan 12 at 5:41
0

No it will be not affect that much on performance & but it does in code readability & maintaining it.

As getConnection in core call getConnectionByName

which code code is checking first if connections array have existed key then it will return that connection so not much load goes in connection but still it will call every time getConnectionName and check $this->connections array so it will affect lil bit..

public function getConnectionByName($connectionName)
    {
        $processConnectionName = $this->getProcessConnectionName($connectionName);
        if (isset($this->connections[$processConnectionName])) {
            return $this->connections[$processConnectionName];
        }

        $connectionConfig = $this->deploymentConfig->get(
            ConfigOptionsListConstants::CONFIG_PATH_DB_CONNECTIONS . '/' . $connectionName
        );

        if ($connectionConfig) {
            $connection = $this->connectionFactory->create($connectionConfig);
        } else {
            throw new \DomainException('Connection "' . $connectionName . '" is not defined');
        }

        $this->connections[$processConnectionName] = $connection;
        return $connection;
    }

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.