Per documentation, using database() in this way:
my $dbh = database({ driver => 'SQLite', database => $filename });
causes a brand new connection to be created each time, and old connections don't get disconnected, so the max gets hits quickly.
The problem appears to be in Database.pm, lines 52-54:
if (ref $arg eq 'HASH') {
$handle_key = $arg;
$conn_details = _merge_settings($arg, $settings, $logger);
} else {
Even though $arg may be the same hash, i.e., { database => "mydb", username="myuser" }, unless special precautions are taken, generating it during runtime creates a different anonymous hashref each time, therefore when used as a key, Perl treats them each as different keys. Since they're different keys, a new connection gets created with each call, and old connections/handles are not reused.