Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 96670fe8

Von Moritz Bunkus vor etwa 8 Jahren hinzugefügt

  • ID 96670fe82a38116ac10592a6ccbd34800f8ad9f8
  • Vorgänger e5a76ed1
  • Nachfolger 0b84f51f

»with_transaction« anstelle von »do_transaction« verwenden

Es sollte so selten wie möglich »do_transaction« verwndet werden, damit
man sich immer angewöhnt, »with_transaction« zu nutzen.

Hintergründe und Unterschiede zwischen den beiden Funktionen sind in der
Dokumentation von SL/DB.pm beschrieben.

Unterschiede anzeigen:

SL/BackgroundJob/CreatePeriodicInvoices.pm
205 205

  
206 206
  my $order   = $config->order;
207 207
  my $invoice;
208
  if (!$self->{db_obj}->db->do_transaction(sub {
208
  if (!$self->{db_obj}->db->with_transaction(sub {
209 209
    1;                          # make Emacs happy
210 210

  
211 211
    $invoice = SL::DB::Invoice->new_from($order);
......
254 254
    _log_msg("_create_invoice created for period start date $period_start_date id " . $invoice->id . " number " . $invoice->invnumber . " netamount " . $invoice->netamount . " amount " . $invoice->amount);
255 255

  
256 256
    # die $invoice->transaction_description;
257

  
258
    1;
257 259
  })) {
258 260
    $::lxdebug->message(LXDebug->WARN(), "_create_invoice failed: " . join("\n", (split(/\n/, $self->{db_obj}->db->error))[0..2]));
259 261
    return undef;
SL/Controller/Buchungsgruppen.pm
85 85
  # allow deletion of unused Buchungsgruppen. Will fail, due to database
86 86
  # constraint, if Buchungsgruppe is connected to a part
87 87

  
88
  my $db = $self->{config}->db;
89
  $db->do_transaction(sub {
90
        my $taxzone_charts = SL::DB::Manager::TaxzoneChart->get_all(where => [ buchungsgruppen_id => $self->config->id ]);
91
        foreach my $taxzonechart ( @{$taxzone_charts} ) { $taxzonechart->delete };
92
        $self->config->delete();
93
        flash_later('info',  $::locale->text('The booking group has been deleted.'));
88
  $self->{config}->db->with_transaction(sub {
89
    my $taxzone_charts = SL::DB::Manager::TaxzoneChart->get_all(where => [ buchungsgruppen_id => $self->config->id ]);
90
    foreach my $taxzonechart ( @{$taxzone_charts} ) { $taxzonechart->delete };
91
    $self->config->delete();
92
    flash_later('info',  $::locale->text('The booking group has been deleted.'));
93

  
94
    1;
94 95
  }) || flash_later('error', $::locale->text('The booking group is in use and cannot be deleted.'));
95 96

  
96 97
  $self->redirect_to(action => 'list');
......
133 134
  my @errors;
134 135

  
135 136
  my $db = $self->config->db;
136
  $db->do_transaction( sub {
137
  if (!$db->with_transaction(sub {
137 138

  
138 139
    $self->config->assign_attributes(%{ $params }); # assign description and inventory_accno_id
139 140

  
......
169 170
        $taxzone_chart->save;
170 171
      }
171 172
    }
172
  } ) || die @errors ? join("\n", @errors) . "\n" : $db->error . "\n";
173
         # die with rollback of taxzone save if saving of any of the taxzone_charts fails
174
         # only show the $db->error if we haven't already identified the likely error ourselves
173

  
174
    1;
175
  })) {
176
    die @errors ? join("\n", @errors) . "\n" : $db->error . "\n";
177
    # die with rollback of taxzone save if saving of any of the taxzone_charts fails
178
    # only show the $db->error if we haven't already identified the likely error ourselves
179
  }
175 180

  
176 181
  flash_later('info', $is_new ? t8('The booking group has been created.') : t8('The booking group has been saved.'));
177 182
  $self->redirect_to(action => 'list');
SL/Controller/CustomerVendor.pm
152 152

  
153 153
  my $db = $self->{cv}->db;
154 154

  
155
  $db->do_transaction(sub {
155
  $db->with_transaction(sub {
156 156
    my $cvs_by_nr;
157 157
    if ( $self->is_vendor() ) {
158 158
      if ( $self->{cv}->vendornumber ) {
......
223 223
        $note->delete(cascade => 'delete');
224 224
      }
225 225
    }
226

  
227
    1;
226 228
  }) || die($db->error);
227 229

  
228 230
}
......
351 353
    $self->action_edit();
352 354
  } else {
353 355

  
354
    $db->do_transaction(sub {
356
    $db->with_transaction(sub {
355 357
      $self->{cv}->delete(cascade => 1);
356 358

  
357 359
      my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
......
379 381
    SL::Helper::Flash::flash('error', $::locale->text('No contact selected to delete'));
380 382
  } else {
381 383

  
382
    $db->do_transaction(sub {
384
    $db->with_transaction(sub {
383 385
      if ( $self->{contact}->used ) {
384 386
        $self->{contact}->detach();
385 387
        $self->{contact}->save();
......
388 390
        $self->{contact}->delete(cascade => 1);
389 391
        SL::Helper::Flash::flash('info', $::locale->text('Contact deleted.'));
390 392
      }
393

  
394
      1;
391 395
    }) || die($db->error);
392 396

  
393 397
    $self->{contact} = $self->_new_contact_object;
......
405 409
    SL::Helper::Flash::flash('error', $::locale->text('No shipto selected to delete'));
406 410
  } else {
407 411

  
408
    $db->do_transaction(sub {
412
    $db->with_transaction(sub {
409 413
      if ( $self->{shipto}->used ) {
410 414
        $self->{shipto}->detach();
411 415
        $self->{shipto}->save(cascade => 1);
......
414 418
        $self->{shipto}->delete(cascade => 1);
415 419
        SL::Helper::Flash::flash('info', $::locale->text('Shipto deleted.'));
416 420
      }
421

  
422
      1;
417 423
    }) || die($db->error);
418 424

  
419 425
    $self->{shipto} = SL::DB::Shipto->new();
SL/Controller/MassInvoiceCreatePrint.pm
56 56
  }
57 57

  
58 58
  my $db = SL::DB::Invoice->new->db;
59
  my @invoices;
59 60

  
60
  if (!$db->do_transaction(sub {
61
    my @invoices;
61
  if (!$db->with_transaction(sub {
62 62
    foreach my $id (@sales_delivery_order_ids) {
63 63
      my $delivery_order    = SL::DB::DeliveryOrder->new(id => $id)->load;
64 64

  
......
66 66
      push @invoices, $invoice;
67 67
    }
68 68

  
69
    my $key = sprintf('%d-%d', Time::HiRes::gettimeofday());
70
    $::auth->set_session_value("MassInvoiceCreatePrint::ids-${key}" => [ map { $_->id } @invoices ]);
71

  
72
    flash_later('info', t8('The invoices have been created. They\'re pre-selected below.'));
73
    $self->redirect_to(action => 'list_invoices', ids => $key);
74

  
75 69
    1;
76 70
  })) {
77 71
    $::lxdebug->message(LXDebug::WARN(), "Error: " . $db->error);
78 72
    $::form->error($db->error);
79 73
  }
74

  
75
  my $key = sprintf('%d-%d', Time::HiRes::gettimeofday());
76
  $::auth->set_session_value("MassInvoiceCreatePrint::ids-${key}" => [ map { $_->id } @invoices ]);
77

  
78
  flash_later('info', t8('The invoices have been created. They\'re pre-selected below.'));
79
  $self->redirect_to(action => 'list_invoices', ids => $key);
80 80
}
81 81

  
82 82
sub action_list_invoices {
SL/Controller/Order.pm
893 893
  my ($self) = @_;
894 894

  
895 895
  my $errors = [];
896
  my $db = $self->order->db;
896
  my $db     = $self->order->db;
897 897

  
898
  $db->do_transaction(
898
  $db->with_transaction(
899 899
    sub {
900 900
      my @spoolfiles = grep { $_ } map { $_->spoolfile } @{ SL::DB::Manager::Status->get_all(where => [ trans_id => $self->order->id ]) };
901 901
      $self->order->delete;
......
915 915
  my ($self) = @_;
916 916

  
917 917
  my $errors = [];
918
  my $db = $self->order->db;
918
  my $db     = $self->order->db;
919 919

  
920
  $db->do_transaction(
921
    sub {
922
      SL::DB::OrderItem->new(id => $_)->delete for @{$self->item_ids_to_delete};
923
      $self->order->save(cascade => 1);
920
  $db->with_transaction(sub {
921
    SL::DB::OrderItem->new(id => $_)->delete for @{$self->item_ids_to_delete};
922
    $self->order->save(cascade => 1);
924 923
  }) || push(@{$errors}, $db->error);
925 924

  
926 925
  return $errors;
SL/Controller/RequirementSpec.pm
121 121
sub action_ajax_save_time_and_cost_estimate {
122 122
  my ($self) = @_;
123 123

  
124
  $self->requirement_spec->db->do_transaction(sub {
124
  $self->requirement_spec->db->with_transaction(sub {
125 125
    # Make Emacs happy
126 126
    1;
127 127
    foreach my $attributes (@{ $::form->{requirement_spec_items} || [] }) {
......
417 417
  }
418 418

  
419 419
  my $db = $self->requirement_spec->db;
420
  if (!$db->do_transaction(sub {
420
  if (!$db->with_transaction(sub {
421 421
    if ($self->copy_source) {
422 422
      $self->requirement_spec($self->copy_source->create_copy(%{ $params }));
423 423
    } else {
424 424
      $self->requirement_spec->save(cascade => 1);
425 425
    }
426
    1;
426 427
  })) {
427 428
    $::lxdebug->message(LXDebug::WARN(), "Error: " . $db->error);
428 429
    @errors = ($::locale->text('Saving failed. Error message from the database: #1', $db->error));
......
635 636
  return $self->js->error(@errors)->render if @errors;
636 637

  
637 638
  my $db = $self->requirement_spec->db;
638
  if (!$db->do_transaction(sub {
639
  if (!$db->with_transaction(sub {
639 640
    $project->save;
640 641
    $self->requirement_spec->update_attributes(project_id => $project->id);
641 642

  
SL/Controller/RequirementSpecItem.pm
87 87
  my $old_type            = $self->item->item_type;
88 88
  my $new_type            = !$dropped_item ? 'section' : $position =~ m/before|after/ ? $dropped_item->item_type : $dropped_item->child_type;
89 89

  
90
  $self->item->db->do_transaction(sub {
90
  $self->item->db->with_transaction(sub {
91 91
    $self->item->remove_from_list;
92 92
    $self->item->parent_id($position =~ m/before|after/ ? $dropped_item->parent_id : $dropped_item->id) if $dropped_item;
93 93
    $self->item->item_type($new_type);
SL/Controller/RequirementSpecPart.pm
73 73
  my ($self) = @_;
74 74

  
75 75
  my $db = $self->requirement_spec->db;
76
  $db->do_transaction(sub {
76
  $db->with_transaction(sub {
77 77
    # Make Emacs happy
78 78
    1;
79 79
    my $parts    = $::form->{additional_parts} || [];
......
81 81
    $_->{position} = $position++ for @{ $parts };
82 82

  
83 83
    $self->requirement_spec->update_attributes(parts => $parts)->load;
84

  
85
    1;
86 84
  }) or do {
87 85
    return $self->js->error(t8('Saving failed. Error message from the database: #1', $db->error))->render;
88 86
  };
SL/Controller/RequirementSpecTextBlock.pm
184 184
  my $dropped_type       = $position ne 'last' ? undef : $::form->{dropped_type} =~ m/^ text-blocks- (?:front|back) $/x ? $::form->{dropped_type} : die "Unknown 'dropped_type' parameter";
185 185
  my $old_where          = $self->text_block->output_position;
186 186

  
187
  $self->text_block->db->do_transaction(sub {
187
  $self->text_block->db->with_transaction(sub {
188 188
    1;
189 189
    $self->text_block->remove_from_list;
190 190
    $self->text_block->output_position($position =~ m/before|after/ ? $dropped_text_block->output_position : $::form->{dropped_type} eq 'text-blocks-front' ? 0 : 1);
......
482 482
sub paste_picture {
483 483
  my ($self, $copied) = @_;
484 484

  
485
  if (!$self->text_block->db->do_transaction(sub {
485
  if (!$self->text_block->db->with_transaction(sub {
486 486
    1;
487 487
    $self->picture($copied->to_object)->save;        # Create new picture from copied data and save
488 488
    $self->text_block->add_pictures($self->picture); # Add new picture to text block
SL/Controller/Taxzones.pm
72 72
  # allow deletion of unused tax zones. Will fail, due to database
73 73
  # constraints, if tax zone is used anywhere
74 74

  
75
  my $db = $self->{config}->db;
76
  $db->do_transaction(sub {
77
        my $taxzone_charts = SL::DB::Manager::TaxzoneChart->get_all(where => [ taxzone_id => $self->config->id ]);
78
        foreach my $taxzonechart ( @{$taxzone_charts} ) { $taxzonechart->delete };
79
        $self->config->delete();
80
        flash_later('info',  $::locale->text('The tax zone has been deleted.'));
75
  $self->{config}->db->with_transaction(sub {
76
    my $taxzone_charts = SL::DB::Manager::TaxzoneChart->get_all(where => [ taxzone_id => $self->config->id ]);
77
    foreach my $taxzonechart ( @{$taxzone_charts} ) { $taxzonechart->delete };
78
    $self->config->delete();
79
    flash_later('info',  $::locale->text('The tax zone has been deleted.'));
80

  
81
    1;
81 82
  }) || flash_later('error', $::locale->text('The tax zone is in use and cannot be deleted.'));
82 83

  
83 84
  $self->redirect_to(action => 'list');
......
121 122
  my @errors;
122 123

  
123 124
  my $db = $self->config->db;
124
  $db->do_transaction( sub {
125
  if (!$db->with_transaction(sub {
125 126

  
126 127
    # always allow editing of description and obsolete
127 128
    $self->config->assign_attributes( %{$params} ) ;
......
160 161
        $taxzone_chart->save;
161 162
      }
162 163
    }
163
  } ) || die @errors ? join("\n", @errors) . "\n" : $db->error . "\n";
164
         # die with rollback of taxzone save if saving of any of the taxzone_charts fails
165
         # only show the $db->error if we haven't already identified the likely error ourselves
164

  
165
    1;
166
  })) {
167
    die @errors ? join("\n", @errors) . "\n" : $db->error . "\n";
168
    # die with rollback of taxzone save if saving of any of the taxzone_charts fails
169
    # only show the $db->error if we haven't already identified the likely error ourselves
170
  }
166 171

  
167 172
  flash_later('info', $is_new ? t8('The taxzone has been created.') : t8('The taxzone has been saved.'));
168 173
  $self->redirect_to(action => 'list');
SL/DB/Helper/ActsAsList.pm
40 40
sub remove_from_list {
41 41
  my ($self) = @_;
42 42

  
43
  my $worker = sub {
43
  return $self->db->with_transaction(sub {
44 44
    remove_position($self);
45 45

  
46 46
    # Set to -1 manually because $self->update_attributes() would
......
56 56
SQL
57 57
    $self->db->dbh->do($sql, undef, $self->$primary_key_col);
58 58
    $self->$column(undef);
59
  };
60

  
61
  return $self->db->in_transaction ? $worker->() : $self->db->do_transaction($worker);
59
  });
62 60
}
63 61

  
64 62
sub add_to_list {
......
109 107
      ${group_by}
110 108
SQL
111 109

  
112
  my $worker = sub {
110
  return $self->db->with_transaction(sub {
113 111
    $self->db->dbh->do($query, undef, $new_position - 1, @values);
114 112
    $self->update_attributes($column => $new_position);
115
  };
116

  
117
  return $self->db->in_transaction ? $worker->() : $self->db->do_transaction($worker);
113
  });
118 114
}
119 115

  
120 116
sub get_next_in_list {
......
144 140

  
145 141
  my $self   = ref($class_or_self) ? $class_or_self : $class_or_self->new;
146 142
  my $column = column_name($self);
147
  my $result = $self->db->do_transaction(sub {
143
  my $result = $self->db->with_transaction(sub {
148 144
    my $query = qq|UPDATE | . $self->meta->table . qq| SET ${column} = ? WHERE id = ?|;
149 145
    my $sth   = $self->db->dbh->prepare($query) || die $self->db->dbh->errstr;
150 146

  
......
153 149
    }
154 150

  
155 151
    $sth->finish;
152

  
153
    1;
156 154
  });
157 155

  
158 156
  return $result;
SL/DB/Invoice.pm
252 252
    $params{ar_id} = $chart->id;
253 253
  }
254 254

  
255
  my $worker = sub {
255
  if (!$self->db->with_transaction(sub {
256 256
    my %data = $self->calculate_prices_and_taxes;
257 257

  
258 258
    $self->_post_create_assemblyitem_entries($data{assembly_items});
......
267 267
    $self->_post_update_allocated($data{allocated});
268 268

  
269 269
    $self->_post_book_rounding($data{rounding});
270
  };
271 270

  
272
  if ($self->db->in_transaction) {
273
    $worker->();
274
  } elsif (!$self->db->do_transaction($worker)) {
271
    1;
272
  })) {
275 273
    $::lxdebug->message(LXDebug->WARN(), "convert_to_invoice failed: " . join("\n", (split(/\n/, $self->db->error))[0..2]));
276 274
    return undef;
277 275
  }
SL/DB/Object.pm
140 140
sub save {
141 141
  my ($self, @args) = @_;
142 142

  
143
  my ($result, $exception);
144
  my $worker = sub {
145
    $exception = $EVAL_ERROR unless eval {
146
      SL::DB::Object::Hooks::run_hooks($self, 'before_save');
147
      $result = $self->SUPER::save(@args);
148
      SL::DB::Object::Hooks::run_hooks($self, 'after_save', $result);
149
      1;
150
    };
143
  my $result;
151 144

  
152
    return $result;
153
  };
145
  $self->db->with_transaction(sub {
146
    SL::DB::Object::Hooks::run_hooks($self, 'before_save');
147
    $result = $self->SUPER::save(@args);
148
    SL::DB::Object::Hooks::run_hooks($self, 'after_save', $result);
154 149

  
155
  $self->db->in_transaction ? $worker->() : $self->db->do_transaction($worker);
156

  
157
  die $exception if $exception;
150
    1;
151
  }) || die $self->error;
158 152

  
159 153
  return $result;
160 154
}
......
162 156
sub delete {
163 157
  my ($self, @args) = @_;
164 158

  
165
  my ($result, $exception);
166
  my $worker = sub {
167
    $exception = $EVAL_ERROR unless eval {
168
      SL::DB::Object::Hooks::run_hooks($self, 'before_delete');
169
      $result = $self->SUPER::delete(@args);
170
      SL::DB::Object::Hooks::run_hooks($self, 'after_delete', $result);
171
      1;
172
    };
173

  
174
    return $result;
175
  };
159
  my $result;
176 160

  
177
  $self->db->in_transaction ? $worker->() : $self->db->do_transaction($worker);
161
  $self->db->with_transaction(sub {
162
    SL::DB::Object::Hooks::run_hooks($self, 'before_delete');
163
    $result = $self->SUPER::delete(@args);
164
    SL::DB::Object::Hooks::run_hooks($self, 'after_delete', $result);
178 165

  
179
  die $exception if $exception;
166
    1;
167
  }) || die $self->error;
180 168

  
181 169
  return $result;
182 170
}
SL/DB/RequirementSpec.pm
145 145
  return $self->_create_copy(%params) if $self->db->in_transaction;
146 146

  
147 147
  my $copy;
148
  if (!$self->db->do_transaction(sub { $copy = $self->_create_copy(%params) })) {
148
  if (!$self->db->with_transaction(sub { $copy = $self->_create_copy(%params) })) {
149 149
    $::lxdebug->message(LXDebug->WARN(), "create_copy failed: " . join("\n", (split(/\n/, $self->db->error))[0..2]));
150 150
    return undef;
151 151
  }
t/ar/ar.t
72 72

  
73 73
  my $db = $invoice->db;
74 74

  
75
  $db->do_transaction( sub {
75
  $db->with_transaction( sub {
76 76

  
77 77
  my $tax = SL::DB::Manager::Tax->find_by(taxkey => 0, rate => 0);
78 78

  
......
91 91

  
92 92
  _save_and_pay_and_check(invoice => $invoice, bank => $bank, pay => 1, check => 1);
93 93

  
94
  1;
95

  
94 96
  }) || die "something went wrong: " . $db->error;
95 97
  return $invoice->invnumber;
96 98
};
......
119 121

  
120 122
  my $db = $invoice->db;
121 123

  
122
  $db->do_transaction( sub {
124
  $db->with_transaction( sub {
123 125

  
124 126
  # TODO: check for currency and exchange rate
125 127

  
......
140 142
  $invoice->create_ar_row( chart => $ar_chart );
141 143
  _save_and_pay_and_check(invoice => $invoice, bank => $bank, pay => 1, check => 1);
142 144

  
145
  1;
143 146
  }) || die "something went wrong: " . $db->error;
144 147
  return $invoice->invnumber;
145 148
};

Auch abrufbar als: Unified diff