Mojolicious :: Lite应用程序可用于morbo,但不能用于催眠药。
my $dbh = DBI->connect("dbi:mysql:dbname=xxx", "uname", "pass",
{ AutoCommit => 0, mysql_enable_utf8 => 1}, )
or die "Couldn't connect to database: ", $DBI::errstr;
helper db => sub { $dbh };
get '/xxx' => sub {
my $sth = $self->db->prepare("insert into posts values(?,?,?,?,?,?)");
$sth->execute('xxx', 'xxx', 'xxx', 'xxx', 'xxx', 'xxx');
$sth->finish();
$self->db->commit;
};
当使用hypnotoad运行时,该应用程序的其余部分正在运行,但未在数据库中读取/写入数据。请帮助我编写可与催眠药一起使用的代码
答案 0 :(得分:1)
您可以像这样使用DBIx :: Connector
use strict;
use warnings;
use Mojolicious::Lite;
use DBIx::Connector;
helper connector => sub {
state $db = DBIx::Connector->new(sprintf('dbi:mysql:host=%s:database=%s',@{ $config->{mysql_database}}{qw[host db]}),@{$config->{mysql_database}}{qw[user password]}) or die "Could not connect";
};
helper mysql => sub { shift->connector->dbh };
post '/login' => sub {
my $c = shift;
my $user = $c->param('user_email');
my $password = $c->param('password');
my $sth = $c->mysql->prepare_cached('SELECT * FROM users WHERE user_email = ?') or $c->app->log->debug($DBI::errstr);
$sth->execute($user);
my $row = $sth->fetchrow_arrayref;
$sth->finish;
## more code
};
答案 1 :(得分:1)
当hypnotoad作为守护程序运行时(因此,是分叉的),则数据库连接丢失。
我使用了数据库插件:
Cell.tsx
在启动阶段:
width: ${p.gutters === "margin" ? `calc(${p.sm.s && calcWidth(p.sm.s)} - 2rem)` : (p.sm.s && calcWidth(p.sm.s))};
最重要的是mysql_auto_reconnect !!!那解决了我的问题。