PostgreSQL connection problem

32 views
Skip to first unread message

Mark Jones

unread,
Aug 22, 2019, 10:59:31 AM8/22/19
to Mojolicious
Hi,

I have an issue with a Mojolicious app that seems to loose connection to a Postgres database whenever the postgres service is restarted. Is this normal behavior, shouldn't the Mojo app automatically reconnect without having to restart hypnotoad?

The error that gets logged is:
[error] no connection to the server at /var/www/mojoApp...

Postgres db connection:

app->attr(db => sub {
    my $self = shift;
    my $dbh = &connect('dbname');
    return $dbh;
});

connect sub:

        my ($dbname) = shift;

        if($dbname && $dbname ne '')
        {
                my $host='xx.xx.xx.xx';
                my $dsn=qq(dbi:Pg:dbname=$dbname;host=$host);
                my $dbusername = xx;
                my $password = xx;
                my $dbh = DBI->connect($dsn,$dbusername,$password)|| die("Connect error: $DBI::errstr");
                return $dbh;
        }

Is there something else that I need to add?

Thanks

Dan Book

unread,
Aug 22, 2019, 11:05:14 AM8/22/19
to mojol...@googlegroups.com
I recommend a connection manager for all DBI usage, and for Mojolicious a great one is Mojo::Pg. A DBI object cannot handle disconnections or reconnecting on fork, but by storing only a Mojo::Pg object, and getting a ->db from it only when you need to run queries, that ->db call can reconnect if needed.

-Dan

--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mojolicious/742b60f5-af10-44db-b303-e1631272ca15%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages