Allow multiple requests from the same PHP user

Asked

Viewed 630 times

0

Hello, I am working on a legacy system developed with PHP. This system acts as a single-page application using jquery.ajax to load the pages the user requests, each page requested from the system is loaded into a new tab. In search pages depending on the filter by the user the processing can take something around 30 or 40 seconds to return due to the data volume in the database. The problem occurs in this case mentioned above where user can’t do anything else in the system until the query request is completed. If the user tries for example to open another page he cannot because the system is still processing the previous request. I wonder if there is any way PHP allows multiple requests from a user, so the system now allows the user to load other pages while the search is requested.

javascript used in the request

        $.ajax({
            type: 'post',
            url: 'php/incluir_pesquisa.php',
            async: true,
            data: {
                tabID: documentoId,
                nomeDocumento: nomeDocumento
            },
            success: function (result) {
                $('#jqxTabs').jqxTabs(
                    'addLast',
                    'Pesquisar',
                    result
                );
            }
        });

2 answers

2


Probably this occurs because of the SESSION or TRANSACTION.

PHP does not in any way limit the number of simultaneous connections (NGINX, Apache or extensions or other firewalls, can even).

1. Session:

Session cannot be accessed by more than one process in write read mode. You can read by numerous processes, but you cannot write.

Soon:

session_start();

$salvaSession = $_SESSION['ultimo_acesso'] = time();
$lerSession = $_SESSION['usuario'];

 // Seu código demorado....
while(1 = 1){ // Um loop infinito para simbolizar algo demorado :P
}

Making a request will prevent any other request from being accepted while the loop is closed.

This is because the session_start indicates that the session can be read and written, which prevents access to any other process.

So to fix use:

session_start();

$salvaSession = $_SESSION['ultimo_acesso'] = time();

session_write_close();

$lerSession = $_SESSION['usuario'];

 // Seu código demorado....
while(1 = 1){ // Um loop infinito para simbolizar algo demorado :P
}

Ready, from the "third line" you already allow another process to have access to the same Sesssion. Because you say to PHP: "I will no longer write anything in this session". You will still be able to read after using the session_write_close();. ;)

Soon, after the session_write_close(); another request can be processed, which does not require ending the loop. : D

2. TRANSACTION:

This is more complex and is related to the database (not PHP). I depend on the level of buffer used in the database or queries.

For example:

mysqli_begin_transaction($con);
mysqli_autocommit($con, false);

mysqli_query($con, 'SELECT * FROM usuarios WHERE id = 1 FOR UPDATE');

 // Seu código demorado....
while(1 = 1){ // Um loop infinito para simbolizar algo demorado :P
}

mysqli_commit($sql);

If you access this page again, in a second tab, it will not be processed, because Mysql will prevent the reading of the query while there is no UPDATE or COMMIT, this will depend on the Engine used, from Banco de dados used and the level of `Isolation!

  • Exactly these bottleneck points I identified in the application. Mainly related to session. Thank you

0

I would not decide to put some action while loading the requests?

$.ajax({
        type : 'POST',
        url  : 'suaPagina.php',
        data : data,
        dataType: 'json',
        beforeSend: function()
        {   
            $(".item").html(''); //<-- ação aqui

        },
    }); 
  • Thanks for the reply Felipe, would not solve, already tried for example cancel the request of the search when the user closes the search tab, but even so the server gets stuck waiting for the answer of the database

  • I understand, but if you open the requests in another tab, wouldn’t it be better to work with GET ? Ajax is recommended at most in modals because it is preloaded before operation, decreasing the performance of pages.

  • Yes, actually this change can improve the performance of the page, but it still does not solve the problem of what happens on the server side, where it does not process a request until the end of the other.

  • Felipe, if the server does not respond, it is no use sending more requests.

  • By the way the problem is in async itself, I have no knowledge in these functions, so I saw it depends on a src="", it may solve your problems

Browser other questions tagged

You are not signed in. Login or sign up in order to post.