In case of customisation we need to initiate read and write connection in our .phtml file.

Should we manually close these connection at the end of file? or Magento self handles it?

In my case I have several time called read connections in my templates now I am feeling there must be any other solutions to handle this.

If I use multiple time Read Connection, would it may load up my database or server?

Somewhere I found it to be used.

Read Connection

$db = Mage::getSingleton('core/resource')->getConnection('core_read');

You can close the connection with this:


And then re-open it with this


Should I do this for handling multiple and concurrent connections?

Let me know if any suggestion.

1 Answer 1


No, you dont have to close the connection .

There should be no read and write calls in .phtml file or template files , it is not best practice in MVC or Magento. You should use model for that .

Mage::getSingleton('core/resource')->getConnection('core_wri‌​te'); gets an already open connection. If you close it, other code will fail to reuse that connection

  • Thanks. If I call create a CMS page by calling php file. I will need some dynamic content to show. I use Mage::getSingleton('core/resource')->getConnection('core_read'); in php file to read data. If the php file is not related with a model. Is there any preferred solution. Commented Apr 19, 2017 at 7:12
  • Share your code , what php file are you talking about ? is it a block ? block will also do if you are not fetching more than 1-2 rows in collection , please accept the answer as it solves you doubt in question Commented Apr 19, 2017 at 8:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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