Is there any way to close database connection after fetch data

Is there any way to close database connection after fetch data

by Santhosh Samban -
Number of replies: 2

I am trying to access value from database in every 2 seconds and simultaneously 50 users.


My query looks as follows:


global $DB;

$curseId = $_REQUEST['crseId'];


if(isset($curseId)){

 

$qstndetails = $DB->get_records_sql('SELECT * FROM {questions} WHERE courseid=? ORDER BY id DESC LIMIT 0,1',array($curseId)); 

$qstnval = array_values($qstndetails);

}


Because of heavy load my data is not loading and I am continuosely fetching it in every 2 seconds.


How can I close data base connection after fetching record?

In case of  java we can close object of Connection class. Suppose con is the object then we close connection after fetching data as con.close();.

Is there any way to close database connection after fetch data in moodle??


Moodle version - 2.9.1

Average of ratings: -
In reply to Santhosh Samban

Re: Is there any way to close database connection after fetch data

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

you haven't really given much context to go on, but the connection should be closed automatically once the PHP request is finished.

It sounds like you really should be looking at caching the result ( https://docs.moodle.org/dev/Cache_API ), rather than doing the DB query multiple times (although 25 DB queries per second sounds well within the capabilities of any reasonable DB server - that's only a couple of page loads for a typical Moodle page).

Whilst you're at it, you should remove the $_REQUEST line and use the Moodle 'optional_param' or 'required_param' function instead.

Also, the code would be a lot more readable if you used $DB->get_records instead of $DB->get_records_sql.

What are you trying to achieve with this - the code appears to be gathering a single question from the course and then converting it into a harder to read format (by converting an object with helpful value names to an array with all the value names stripped out).

In reply to Santhosh Samban

Re: Is there any way to close database connection after fetch data

by Tomasz Muras -
Picture of Core developers Picture of Plugin developers Picture of Plugins guardians Picture of Translators

Try:

$DB->dispose()