Skip to content

Core\Db\Connection::close

$connection->close(): void

Gives a database connection back before the program has finished.

A server keeps a small number of connections for each database. Your program borrows one when it opens the database, and gives it back when the request ends. close gives it back earlier, so another request can use it while your program does the rest of its work.

After close, the connection is no longer usable. Every method that needs it throws a LogicError. isOpen still works, and it returns false. A second close does nothing and is not an error, so a cleanup step may run twice.

The name is free again after a close. Core\Db::connect with the same name opens the database and gives you a new connection.

Return value

Returns void. Nothing. The only effect is that the connection goes back to the pool, and a program cannot use the pool directly.

Examples

The examples below run with this configuration file.

nvs.toml
# The `notes` database is kept in memory. It is deleted when the program ends.
[db.notes]
driver = "sqlite"
path = ":memory:"
# The program may open the `notes` database.
[capabilities.db]
connect = ["notes"]

Give the connection back when the work is done

<?nvs
// Shows the usual place for a close: after the last statement. The connection
// goes back to the server, and another request can use it.
var $db = Core\Db::connect('notes');
$db->execute('create table notes (id integer primary key, text text not null)', []);
$db->execute('insert into notes (id, text) values (?, ?)', [1, 'buy milk']);
// This prints `buy milk`.
foreach ($db->query('select text from notes order by id', []) as Core\Db\Row $row) {
echo $row->string('text') ?? '', "\n";
}
// The program needs the database no longer.
$db->close();
// `isOpen` is the one method a closed connection still answers. This prints `closed`.
echo $db->isOpen() ? 'open' : 'closed', "\n";
Output
buy milk
closed

A second close does nothing

<?nvs
// Shows that a second close is allowed. A step that ends early can close the
// database, and the cleanup at the end can close it again.
class Import
{
public static function run(Core\Db\Connection $db, array<string> $lines): int
{
int $written = 0;
foreach ($lines as string $line) {
if (Core\Str::length($line) == 0) {
// An empty line ends the import. Nothing after it needs the database.
$db->close();
return $written;
}
$db->execute('insert into notes (text) values (?)', [$line]);
$written = $written + 1;
}
return $written;
}
}
var $db = Core\Db::connect('notes');
$db->execute('create table notes (id integer primary key, text text not null)', []);
// This prints `2 lines written`.
echo Import::run($db, ['buy milk', 'call the plumber', '', 'not read']), " lines written\n";
// The import closed the connection already. This close does nothing, so the
// program does not have to test the connection first. This prints `closed`.
$db->close();
echo $db->isOpen() ? 'open' : 'closed', "\n";
Output
2 lines written
closed

Free the connection before the slow step

<?nvs
// Shows a program that gives the connection back before its slowest step. It
// reads everything it needs, closes the connection, and writes its report
// afterwards. Another request can use the connection while the report is built.
var $db = Core\Db::connect('notes');
$db->execute('create table orders (id integer primary key, city text not null, total int not null)', []);
$db->execute('insert into orders (city, total) values (?, ?)', ['Bremen', 1200]);
$db->execute('insert into orders (city, total) values (?, ?)', ['Lisbon', 800]);
array<tainted string> $cities = [];
array<int> $totals = [];
foreach ($db->query('select city, total from orders order by id', []) as Core\Db\Row $row) {
$cities[] = $row->string('city') ?? '';
$totals[] = $row->int('total') ?? 0;
}
// The report needs no more rows, so the database is free from here on.
$db->close();
// This part can take a while in a real program. It runs without a connection.
int $sum = 0;
foreach ($totals as int $total) {
$sum = $sum + $total;
}
// This prints `2 cities, 2000 in total`.
echo Core\Arr::count($cities), ' cities, ', $sum, " in total\n";
Output
2 cities, 2000 in total