connect to database inside request
authorDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 1 Nov 2018 10:09:30 +0000 (11:09 +0100)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 1 Nov 2018 10:09:30 +0000 (11:09 +0100)
this ensures that we have database connection if it restarts

barcode-seen.pl

index 7c651c9..0770ba2 100755 (executable)
@@ -11,8 +11,14 @@ use C4::Circulation;
 use Data::Dump qw(dump);
 
 
-my $dbh = C4::Context->dbh;
-my $sth_items = $dbh->prepare(qq{
+
+get '/' => sub {
+       my $c = shift;
+
+       my $barcode = $c->param('barcode');
+
+       my $dbh = C4::Context->dbh;
+       my $sth_items = $dbh->prepare(qq{
 select
        itemnumber,
        items.biblionumber,
@@ -22,6 +28,13 @@ from items
 join biblio on biblio.biblionumber = items.biblionumber where barcode = ?
 });
 
+       warn "# $barcode";
+       $sth_items->execute( $barcode );
+       while ( my $row = $sth_items->fetchrow_hashref ) {
+               warn "# items row ",dump($row);
+               $c->stash( $_ => $row->{$_} ) foreach keys %$row;
+       }
+
 my $sth_update = $dbh->prepare(qq{
 update items set datelastseen = now() where barcode = ?
 });
@@ -34,18 +47,6 @@ my $sth_issues = $dbh->prepare(qq{
 select firstname,surname,userid,email from issues join borrowers on issues.borrowernumber = borrowers.borrowernumber where itemnumber = ?
 });
 
-
-get '/' => sub {
-       my $c = shift;
-
-       my $barcode = $c->param('barcode');
-       warn "# $barcode";
-       $sth_items->execute( $barcode );
-       while ( my $row = $sth_items->fetchrow_hashref ) {
-               warn "# items row ",dump($row);
-               $c->stash( $_ => $row->{$_} ) foreach keys %$row;
-       }
-
        $sth_update->execute( $barcode );
        $sth_inventura->execute( $barcode );