2

I am calling Db::getInstance()->execute() for insert and update and for selection query Db::getInstance()->executeS() After this do we need to close the database conenction or prestashop itself closes /handles the database connection .

how will we check whether a connection is open for a longtime for our database in prestahsop? Our server admin says our database sessions are getting increased since we are not closing the connection .

Any expert prestahsop can you explain how PrestaShop closes the connection or do we need to close it manually ?

 Db::getInstance()->execute()

For certain cronjob files that we created ,what we do is we include the config file as shown below: require 'config/config.inc.php';

    $db = Db::getInstance();
     $result = $db->executeS($get_promotions_sql, false);   
     while ($row = $db->nextRow($result)) {
       $id_product=$row['id_product'];
       $name="<br>".$row['name'];
       }

does this make my database connection open for ever? or will it be closed/or handle by getInstance function in prestashop

Jerry Abraham
  • 1,039
  • 18
  • 42

2 Answers2

3

You are not supposed to close the DB connection, PrestaShop will handle that automatically. You can also disconnect the connection manually by calling the following code:

Db::getInstance()->disconnect();

If you are having the performance issue on your store then you can enable PrestaShop Code Profiler and see what Object instances are using your server memory.

In order to enable PrestaShop Code Profiler, you need to follow steps below:

  • On your server, edit the file /config/defines.inc.php.

  • Find this line (around line 43): define('_PS_DEBUG_PROFILING_',
    false);

  • In this line, change "false" to "true".

  • Save your changes.

Knowband Plugins
  • 1,297
  • 1
  • 7
  • 6
  • `disconnect()` is not a static method, but can be called with `Db::getInstance()->disconnect()`. – TheDrot Nov 09 '16 at 06:08
1

Looking at the Db class...

/**
 * Close connection to database
 */
public function __destruct()
{
    if ($this->link)
        $this->disconnect();
}

Connection to the database will be closed when destructor is called.

Can I trust PHP __destruct() method to be called?

Connection is established when constructor is called. So no, you don't need to manually open/close connection to database.

Community
  • 1
  • 1
TheDrot
  • 4,246
  • 1
  • 16
  • 26
  • What abt the db sessions ?how can I check why it increases because of my code?In my external page that does not contain any class ,I am simply adding the header and calling db instance here do I need to call the destructor? here is my code http://pastie.org/10956905 – Jerry Abraham Nov 06 '16 at 13:39
  • @JerryAbraham As explained in the answer link, destructor will get called when no references exist anymore for object or when script execution ends. Here's another [link](http://stackoverflow.com/questions/2385047/when-will-destruct-not-be-called-in-php) which explains where destructor wouldn't get called. – TheDrot Nov 09 '16 at 06:14