Hello, everyone! 
I'm having a problem with starting my app via hypnotoad. While in simple 
daemon (or morbo) mode everything works fine, app started by hypnotoad is 
failed to connect to SQL. I read 
https://github.com/kraih/mojo/wiki/Hypnotoad-prefork-web-server and tried 
to use one of recipe: DBIx::Connector instead of usual DBI->connect, with 
no difference: app is still can't connect to SQL in prefork mode. 

The other recipe (with has => dbh) is quite hard to implement without 
rewriting all parts of the project, and therefore i decided first  to ask, 
maybe someone already solved this problem. 

So app is basically consist of: 

script/portmgr 
lib/PortMgr.pm
lib/PortMgr/Controller/Hw.pm
lib/PortMgr/Model/Hw.pm
lib/PortMgr/Storage/PgSQL.pm

chain of calls is like: 

in lib/PortMgr.pm: 

$r->get('/')->to('hw#mainpage')->name('main');

then to controller: 

= lib/PortMgr/Controller/Hw.pm ============================
package PortMgr::Controller::Hw;
use Mojo::Base 'Mojolicious::Controller';
use PortMgr::Model::Hw;

my $hwmodel = PortMgr::Model::Hw->new();

sub mainpage {
    my $self = shift;
    my $allhw = $hwmodel->get_hw();
    $self->render(hw => $allhw);
}
===================================================

= lib/PortMgr/Model/Hw.pm ===============================
package PortMgr::Model::Hw;
use PortMgr::Storage::PgSQL;

my $storage = PortMgr::Storage::PgSQL->new();

sub new {
    my $self = shift;
    return $self;
}

sub get_hw {
    my $self = shift;
    my $hw_id = shift;
    my $data = $storage->query(
        table => ...,
        fields => ....,
        join => ...,
        where => ...,
    );
    return $data;
}
===================================================


= lib/PortMgr/Storage/PgSQL.pm ===========================
package PortMgr::Storage::PgSQL;
use DBI;
use DBIx::Connector;

# Singleton

our $instance = undef;

sub new {
    my $class = shift;
    return $instance if defined $instance;
    my $self = {};
    $instance = bless ($self, $class);
    $self->{db} = $instance->init();
    return $instance;
}

sub init {
    my $self = shift;
    my $db = shift;
    $self->{db} = 
DBIx::Connector->connect("dbi:Pg:dbname=ports;host=127.0.0.10", "pgsql", 
"", {AutoCommit => 1, RaiseError => 1});
    if (!$self->{db}) {
        $log->error("INIT Error: " . $DBI::errstr);
        return undef;
    }
    return $self->{db};
}

sub query {
    my $self = shift;
    my $opt = { @_ };
....
....
    my $q = "SELECT $sql_fields FROM $table $sql_join $sql_where $sql_group 
$sql_order";

    return $self->{db}->selectall_hashref($q,$key);
}

===================================================

When in development mode or non-forked mode of hypnotoad, everything works 
fine, but in prefork mode I get error: 

DBD::Pg::db selectall_hashref failed: server closed the connection 
unexpectedly. 

And if I try to use recipe with "has => dbh", somehow i need to get access 
to $self->app in lib/PortMgr/Storage/PgSQL.pm, but it is not a Mojolicious 
controller class, so i need to drag $dbh from lib/PortMgr.pm to 
lib/PortMgr/Controller/Hw.pm to lib/PortMgr/Model/Hw.pm to 
lib/PortMgr/Storage/PgSQL.pm? 

Is there any other solution? 

-- 
With regards,
Alexander Lunev

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Reply via email to