Exceptions
Nette Database uses an exception hierarchy. The base class is Nette\Database\DriverException, which extends
PDOException and provides enhanced functionality for working with database errors:
- The
getDriverCode()method returns the error code from the database driver. - The
getSqlState()method returns the SQLSTATE code. - The
getQueryString()andgetParameters()methods allow retrieving the original query and its parameters.
The DriverException class is extended by the following specialized exceptions:
ConnectionException– indicates a failure to connect to the database server.ConnectionLostException– the connection was dropped during an operation (server restart, network failure, idle timeout); a reconnect is required before further use.
ConstraintViolationException– the base class for database constraint violations, from which the following exceptions inherit:ForeignKeyConstraintViolationException– violation of a foreign key constraint.NotNullConstraintViolationException– violation of a NOT NULL constraint.UniqueConstraintViolationException– violation of a uniqueness constraint.CheckConstraintViolationException– violation of a CHECK constraint.
DeadlockException– a deadlock or serialization failure detected by the server; the transaction was rolled back and may be retried.LockTimeoutException– a lock-wait timeout was exceeded; the statement was aborted, but the surrounding transaction typically remains open.
DeadlockException and LockTimeoutException implement the marker interface
Nette\Database\RetryableException, indicating that the operation can be safely retried automatically – see transactions.
The following example demonstrates how to catch a UniqueConstraintViolationException, which occurs when trying to
insert a user with an email that already exists in the database (assuming the email column has a unique index):
try {
$database->query('INSERT INTO users', [
'email' => 'john@example.com',
'name' => 'John Doe',
'password' => $hashedPassword,
]);
} catch (Nette\Database\UniqueConstraintViolationException $e) {
echo 'A user with this email already exists.';
} catch (Nette\Database\DriverException $e) {
echo 'An error occurred during registration: ' . $e->getMessage();
}
For transient errors such as deadlocks, you can wrap the operation in transaction() with
multiple attempts. If a DeadlockException (or any other RetryableException) occurs, the whole callback
is automatically replayed:
$database->transaction(function ($database) {
$database->query('UPDATE accounts SET balance = balance - ? WHERE id = ?', 100, 1);
$database->query('UPDATE accounts SET balance = balance + ? WHERE id = ?', 100, 2);
}, attempts: 3);