Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php session freezes the whole server when importing to mysql

Tags:

php

session

The title might be a little odd but I can't really describe it any other way. I have the following code:

error_reporting (E_ALL ^ E_NOTICE);

require_once("required/config.php");

$mysqli = new mysqli($db_server, $db_username, $db_password, $db_database);
for ($i = 1; $i < 1001; $i++) {
    if ($insertItem = $mysqli->prepare("INSERT INTO testtable (dummyfield) VALUES (?)")) {
        $insertItem->bind_param('s', $value);

        $value      = "Just some data #$i";

        $insertItem->execute();
        $gebruikersId = $mysqli->insert_id;
        $insertItem->close();

        echo $value . "<br />";
    }
}
$mysqli->close();

When I run this code it takes about 20 - 30 seconds. That's fine. I open a second window and I can browse to and from every other page from the website. No problem here.

However, when I add session_start(); right below error_reporting other pages won't load until the for-loop / importing has finished.

Any ideas? I've tried Google but couldn't find any relevant results.

like image 665
David Janssen Avatar asked Jan 13 '23 16:01

David Janssen


1 Answers

As long as one script “uses” the session, the session file is locked – and other scripts wanting to use the same session have to wait until the lock is released.

Avoid this by calling session_write_close() as early as possible in your long-running script.

like image 66
CBroe Avatar answered Jan 16 '23 04:01

CBroe