Laravel 会自动关闭数据库连接吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28811274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:03:17  来源:igfitidea点击:

Does Laravel close automatically the DB connections?

phpmysqllaravellaravel-4eloquent

提问by saulob

I'm working with Laravel and Databases (DB) using Eloquent and MySQL.

我正在使用 Eloquent 和 MySQL 处理 Laravel 和数据库 (DB)。

I already found the code to close the DB connection, but I was searching on the Documentation if Laravel close all the connections automatically after any use of it.

我已经找到了关闭数据库连接的代码,但是我在文档中搜索 Laravel 是否在使用它后自动关闭所有连接。

Before using Laravel, every time I used a MySQL command I opened the database connection on the page header and closed it on the page footer.

在使用 Laravel 之前,每次使用 MySQL 命令时,我都会在页眉上打开数据库连接并在页脚上关闭它。

But, what about using Laravel/Eloquent? Does it close? Or do I need to close manually all the connections after using it?

但是,使用 Laravel/Eloquent 怎么样?关门了吗?还是需要在使用后手动关闭所有连接?

Like, basic example using Laravel/Eloquent:

例如,使用 Laravel/Eloquent 的基本示例:

$user = new User;
$user->name = 'John';
$user->save();

I know that on the beginning Laravel opens a connection to the Database/User table. But after saving, does it close it?

我知道一开始 Laravel 会打开一个到数据库/用户表的连接。但是保存后,会关闭吗?

回答by niahoo

Yes.

是的。

With PHP and with Laravel connections are automatically closed at the end of the script.

使用 PHP 和使用 Laravel 时,连接会在脚本结束时自动关闭。