From f82e94c7cbbea66d902e286c531d2282fc69f67c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sven=20Sch=C3=B6ling?= Date: Thu, 8 Sep 2016 16:51:09 +0200 Subject: [PATCH] PE: single-dbh disconnects --- SL/PE.pm | 62 ++++++++++++++++++++++++-------------------------------- 1 file changed, 27 insertions(+), 35 deletions(-) diff --git a/SL/PE.pm b/SL/PE.pm index 2eb720150..c6de663e1 100644 --- a/SL/PE.pm +++ b/SL/PE.pm @@ -37,6 +37,7 @@ package PE; use Data::Dumper; use SL::DBUtils; +use SL::DB; use strict; @@ -46,7 +47,7 @@ sub partsgroups { my ($self, $myconfig, $form) = @_; # connect to database - my $dbh = $form->dbconnect($myconfig); + my $dbh = SL::DB->client->dbh; my ($where, @values); @@ -77,8 +78,6 @@ sub partsgroups { $form->{item_list} = selectall_hashref_query($form, $dbh, $query, @values); - $dbh->disconnect; - $main::lxdebug->leave_sub(); return scalar(@{ $form->{item_list} }); @@ -90,7 +89,7 @@ sub save_partsgroup { my ($self, $myconfig, $form) = @_; # connect to database - my $dbh = $form->dbconnect($myconfig); + my $dbh = SL::DB->client->dbh; $form->{discount} /= 100; @@ -105,8 +104,6 @@ sub save_partsgroup { } do_query($form, $dbh, $query, @values); - $dbh->disconnect; - $main::lxdebug->leave_sub(); } @@ -116,7 +113,7 @@ sub get_partsgroup { my ($self, $myconfig, $form) = @_; # connect to database - my $dbh = $form->dbconnect($myconfig); + my $dbh = SL::DB->client->dbh; my $query = qq|SELECT pg.*, | . @@ -130,8 +127,6 @@ sub get_partsgroup { map({ $form->{$_} = $ref->{$_} } keys(%{$ref})); $sth->finish; - $dbh->disconnect; - # also not orphaned if partsgroup is selected for a cvar filter if ($form->{orphaned}) { my $cvar_count = scalar( @{ SL::DB::PartsGroup->new(id => $form->{id})->custom_variable_configs } ); @@ -147,14 +142,15 @@ sub delete_tuple { my ($self, $myconfig, $form) = @_; # connect to database - my $dbh = $form->dbconnect($myconfig); - - my $table = $form->{type} eq "pricegroup" ? "pricegroup" : "partsgroup"; + SL::DB->client->with_transaction(sub { + my $dbh = SL::DB->client->dbh; - my $query = qq|DELETE FROM $table WHERE id = ?|; - do_query($form, $dbh, $query, $form->{id}); + my $table = $form->{type} eq "pricegroup" ? "pricegroup" : "partsgroup"; - $dbh->disconnect; + my $query = qq|DELETE FROM $table WHERE id = ?|; + do_query($form, $dbh, $query, $form->{id}); + 1; + }) or do { die SL::DB->client->error }; $main::lxdebug->leave_sub(); } @@ -168,7 +164,7 @@ sub pricegroups { my ($self, $myconfig, $form) = @_; # connect to database - my $dbh = $form->dbconnect($myconfig); + my $dbh = SL::DB->client->dbh; my ($where, @values); @@ -203,8 +199,6 @@ sub pricegroups { $form->{item_list} = selectall_hashref_query($form, $dbh, $query, @values); - $dbh->disconnect; - $main::lxdebug->leave_sub(); return scalar(@{ $form->{item_list} }); @@ -218,23 +212,23 @@ sub save_pricegroup { my ($self, $myconfig, $form) = @_; - # connect to database - my $dbh = $form->dbconnect($myconfig); - my $query; - - $form->{discount} /= 100; + SL::DB->client->with_transaction(sub { + my $dbh = SL::DB->client->dbh; + my $query; - my @values = ($form->{pricegroup}); + $form->{discount} /= 100; - if ($form->{id}) { - $query = qq|UPDATE pricegroup SET pricegroup = ? WHERE id = ? |; - push(@values, $form->{id}); - } else { - $query = qq|INSERT INTO pricegroup (pricegroup) VALUES (?)|; - } - do_query($form, $dbh, $query, @values); + my @values = ($form->{pricegroup}); - $dbh->disconnect; + if ($form->{id}) { + $query = qq|UPDATE pricegroup SET pricegroup = ? WHERE id = ? |; + push(@values, $form->{id}); + } else { + $query = qq|INSERT INTO pricegroup (pricegroup) VALUES (?)|; + } + do_query($form, $dbh, $query, @values); + 1; + }) or do { die SL::DB->client->error }; $main::lxdebug->leave_sub(); } @@ -248,7 +242,7 @@ sub get_pricegroup { my ($self, $myconfig, $form) = @_; # connect to database - my $dbh = $form->dbconnect($myconfig); + my $dbh = SL::DB->client->dbh; my $query = qq|SELECT id, pricegroup FROM pricegroup WHERE id = ?|; my $sth = prepare_execute_query($form, $dbh, $query, $form->{id}); @@ -272,8 +266,6 @@ sub get_pricegroup { ($form->{orphaned}) = selectrow_query($form, $dbh, $query, @values); $form->{orphaned} = !$form->{orphaned}; - $dbh->disconnect; - $main::lxdebug->leave_sub(); } -- 2.20.1