Skip to content

Core\Db\Connection::isOpen

$connection->isOpen(): bool

Says whether this connection can still be used.

Core\Db\Connection::isOpen returns true for a connection that is still yours, and false once Core\Db\Connection::close has given it back. It takes no arguments and never throws.

It is the one method a closed connection still answers. Every other method of Core\Db\Connection throws a LogicError after the connection is closed, because the connection it needs is gone. So a program that is not sure asks this first, instead of running a statement and catching the error.

Most programs never need it. It is for the step that may run after another step already finished with the database: a cleanup, a last log line, a handler that receives a connection it did not open.

The examples ask before and after a close, show the error every other method throws, and write a last row only when the database is still there.

Return value

Returns bool. true if statements can still run on the connection. false if close has returned it to the pool.

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"]

Ask whether the connection is still open

<?nvs
// Shows the two answers. A connection is open from the moment you get it, and
// closed once you give it back.
var $db = Core\Db::connect('notes');
// This prints `open`.
echo $db->isOpen() ? 'open' : 'closed', "\n";
$db->close();
// This prints `closed`.
echo $db->isOpen() ? 'open' : 'closed', "\n";
Output
open
closed

The one question a closed connection answers

<?nvs
// Shows why this method exists. Every other method of a closed connection
// throws a `LogicError`. `Core\Db\Connection::isOpen` answers `false` instead,
// so a program can test the connection without catching an error.
var $db = Core\Db::connect('notes');
$db->execute('create table notes (id integer primary key, text text not null)', []);
$db->close();
// This prints `the connection is gone`.
if ($db->isOpen()) {
echo "the connection is here\n";
} else {
echo "the connection is gone\n";
}
// The same question asked by running a statement. This prints `LogicError`.
try {
$db->execute('insert into notes (text) values (?)', ['buy milk']);
} catch (LogicError $error) {
echo "LogicError\n";
}
Output
the connection is gone
LogicError

Write the last row only if the database is there

<?nvs
// Shows the cleanup step of a job. An earlier step may have finished with the
// database and closed it, so the last step asks before it writes.
class Job
{
// Writes a line saying the job is done, if there is still a connection.
// Returns the number of rows written.
public static function finish(Core\Db\Connection $db, string $note): int
{
if (!$db->isOpen()) {
return 0;
}
var $written = $db->execute('insert into log (note) values (?)', [$note]);
return $written->affected() as int;
}
}
var $db = Core\Db::connect('notes');
$db->execute('create table log (id integer primary key, note text not null)', []);
// The database is still here, so the line is written. This prints `1 written`.
echo Job::finish($db, 'import done'), " written\n";
$db->close();
// The connection is gone, so nothing is written and nothing throws. This
// prints `0 written`.
echo Job::finish($db, 'export done'), " written\n";
Output
1 written
0 written