Problem with Hypnotoad/DBI connection

1,016 views
Skip to first unread message

Łukasz Lewandowski

unread,
Mar 14, 2011, 8:53:05 AM3/14/11
to mojol...@googlegroups.com
Hi!

I'm making a new version of http://explain.depesz.com
(migration from Catalyst to Mojolicious, new layout...).

I have a strange problem with DBI connection. When I start application:

./explain.pl daemon [args]

everything works fine!
I've never had any problems during the developement...

But, when I try to use hypnotoad:

./hypnotoad --foreground --config=hypnotoad.conf explain.pl

i got:

Server available at http://*:3020.

DBD::Pg::st execute failed: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request. at {PATH}/Explain/Plugin/Database.pm

BD::Pg::st fetchrow_array failed: no statement executing

...

Application code can be found at:

http://svn.depesz.com/svn/Explain_Depesz_Com/branches/metys/

Any ideas?

Thx

Sebastian Riedel

unread,
Mar 14, 2011, 9:09:14 AM3/14/11
to mojol...@googlegroups.com
Hypnotoad is a preforking web server that starts the application before spawning worker processes (to benefit from copy-on-write).

I suspect you are instantiating your database handle directly on startup and a single connection gets inherited by every single worker process, which doesn't work.

You need a connection manager that is fork aware like DBIx::Connector or make handle instantiation lazy.
Here's a little example.


package Mojolicious::Plugin::Database;
use Mojo::Base 'Mojolicious::Plugin';
use DBI;

has dbh => sub { DBI->connect(...) };
...
sub register {
my ($self, $app) = @_;
...
$app->helper(database => sub { $self->dbh });
...
}
...

P.S.: Can somebody please write a general purpose plugin for this so i never have to explain it again? :)

--
Sebastian Riedel
http://mojolicio.us
http://twitter.com/kraih
http://blog.kraih.com

> --
> You received this message because you are subscribed to the Google Groups "Mojolicious" group.
> To post to this group, send email to mojol...@googlegroups.com.
> To unsubscribe from this group, send email to mojolicious...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mojolicious?hl=en.
>

Vick Khera

unread,
Mar 14, 2011, 9:16:33 AM3/14/11
to mojol...@googlegroups.com
On Mon, Mar 14, 2011 at 9:09 AM, Sebastian Riedel <kra...@googlemail.com> wrote:
> You need a connection manager that is fork aware like DBIx::Connector or make handle instantiation lazy.
[ ... ]

> P.S.: Can somebody please write a general purpose plugin for this so i never have to explain it again? :)

:)

Gave me a chuckle... this same problem was common 13 years ago when
mod_perl was first getting popular.

Abhijit Menon-Sen

unread,
Mar 14, 2011, 9:25:28 AM3/14/11
to mojol...@googlegroups.com
At 2011-03-14 14:09:14 +0100, kra...@googlemail.com wrote:
>
> P.S.: Can somebody please write a general purpose plugin for this so i
> never have to explain it again? :)

Not a plugin, but I wrote this a while ago for the same reason:

http://toroid.org/ams/etc/mojolicious-db-handles

-- ams

Łukasz Lewandowski

unread,
Mar 14, 2011, 9:26:02 AM3/14/11
to mojol...@googlegroups.com
"I suspect you are instantiating your database handle directly on startup"

Something like that (not exactly in "startup" but at the begining of "register").
My mistake...
Thx!



2011/3/14 Sebastian Riedel <kra...@googlemail.com>

Yuki Kimoto

unread,
Mar 14, 2011, 9:59:55 AM3/14/11
to mojol...@googlegroups.com
See

https://github.com/kraih/mojo/wiki/Hypnotoad-prefork-web-server
topic of Database connection problem in preforking
I wrote some days ago.

2011/3/14 Łukasz Lewandowski <lukasz.metys...@gmail.com>:

Ben van Staveren

unread,
Mar 14, 2011, 12:41:00 PM3/14/11
to mojol...@googlegroups.com

> P.S.: Can somebody please write a general purpose plugin for this so i never have to explain it again? :)
>
Should be on CPAN soon as Mojolicious::Database::Plugin by yours truly. I have
to admit I have no idea if it actually does what it advertises to do but
figured might as well do something :P

--
Ben van Staveren
phone: +62 81 70777529
email: benvans...@gmail.com

Konstantin Tokar

unread,
Oct 27, 2012, 8:23:47 PM10/27/12
to mojol...@googlegroups.com, kra...@googlemail.com
In the topic Connecting to the database I propose another way to connect to the database, and I explained some  arguments in its favor.

Konstantin Tokar

unread,
Oct 27, 2012, 8:26:11 PM10/27/12
to mojol...@googlegroups.com, kra...@googlemail.com
Sorry, link to the topic should be https://groups.google.com/d/msg/mojolicious/5AQ_0vowKEs/AmrNhoEwy84J

воскресенье, 28 октября 2012 г., 4:23:47 UTC+4 пользователь Konstantin Tokar написал:
Reply all
Reply to author
Forward
0 new messages