Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mimi0110's avatar

does laravel disconnect to database after each query?

I check on DB by command: show full processlist and see some sleep process. I read some blog and they said laravel and php always disconect DB after finish query. But in my DB still have some long time sleep process .

https://gyazo.com/38c4c3765b00a1a87455e4c57f35e782

I know that I can set wait_timeout to reduce the sleep connection. But my question is : Why laravel doesn't disconnect after each query Please give me the answer . Thank you so much !

0 likes
14 replies
bugsysha's avatar

It doesn't have to be Laravel. Maybe some package is causing that. Or maybe your code. Who knows. List is endless. Are there multiple apps connecting to the same database? Hard to tell anything from what you've provided.

1 like
bugsysha's avatar

But in my laravel code, I do not have any custom code to keep the connection running long time.

Deploy clean Laravel app to use that same database. Add few models and run few cron jobs that access the database. Then see if you have additional sleeping processes. Maybe there is a bug in Laravel, but I doubt that cause there are so many developers and projects using it that it would not go unnoticed for a long time.

1 like
mimi0110's avatar

Deploy clean Laravel app to use that same database. Add few models and run few cron jobs that access the database.

That sound good idea for investigate. it may take time now , also my issue come from production env. the sleep connection may not consum more CPU RDS but it occupied the connection in max connection we have..

kima's avatar

I guess its a PDO behavior.

how many sleep lines you see in the processlist, against how many PHP processes do you have?

1 like
mimi0110's avatar

around 40 sleep process always keep on my DB.

against how many PHP processes do you have

How to know that?

trin's avatar

run

dd(DB::getPdo());

and look to PERSISTENT attribute. seems to laravel use persistent connection

1 like
mimi0110's avatar

It may not related to Pdo, because persitent is false :(

PDOConnection {#966
  inTransaction: false
  attributes: {
    CASE: NATURAL
    ERRMODE: EXCEPTION
    AUTOCOMMIT: 1
    PERSISTENT: false
    DRIVER_NAME: "mysql"
    SERVER_INFO: "Uptime: 333968  Threads: 13  Questions: 4355626  Slow queries: 0  Opens: 644  Flush tables: 1  Open tables: 570  Queries per second avg: 13.042"
    ORACLE_NULLS: NATURAL
    CLIENT_VERSION: " "
    SERVER_VERSION: "5.7.12"
    STATEMENT_CLASS: array:2 [
      0 => "Doctrine\DBAL\Driver\PDOStatement"
      1 => []
    ]
    EMULATE_PREPARES: 0
    CONNECTION_STATUS: " via TCP/IP"
    DEFAULT_FETCH_MODE: BOTH
  }
}

anything else can I check?

trin's avatar

stop php-fpm, restart mysql, look for sleep process. start php-fpm, look for sleep process. update some pages, look for sleep process. i think it php mechanic, but it not are problem.

1 like
mimi0110's avatar

what things should I check with this funtion?

phpinfo()

mimi0110's avatar

did you mean mysqli.allow_persistent ? I checked and the value is: mysqli.allow_persisten: On so does it related to sleep connection?

Please or to participate in this conversation.