Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env perl
- use FindBin '$Bin';
- use lib "$Bin/local/lib/perl5", "$Bin/lib";
- use v5.16;
- use warnings;
- use My::Schema;
- use Data::Dumper;
- my $schema = My::Schema->my_connect;
- my $COSTS = $schema->resultset('Cost');
- sub create {
- my ($amount) = @_;
- $amount //= 0;
- $COSTS->create({
- resource_id => 1,
- amount => $amount,
- currency => 'EUR',
- recurrence => 'monthly',
- comment => 'hi',
- });
- }
- eval {
- $schema->txn_do(sub {
- say 'creating record 1';
- create(1);
- say 'created rec 1';
- say 'disconnecting';
- $schema->storage->dbh->disconnect;
- say 'creating record 2';
- create(2);
- say 'created rec 2';
- });
- };
- if (my $e = $@) {
- say 'caught error';
- say Dumper($e);
- }
- say 'creating rec 3';
- create(3);
- __END__
- Output follows:
- creating record 1
- created rec 1
- disconnecting
- creating record 2
- creating record 1
- created rec 1
- disconnecting
- creating record 2
- caught error
- $VAR1 = bless( {
- 'msg' => '{UNKNOWN}: Transaction aborted: DBIx::Class::Storage::DBI::_dbh_execute(): DBI Exception: DBD::mysql::st execute failed: MySQL server has gone away [for Statement "INSERT INTO cost ( amount, comment, currency, recurrence, resource_id) VALUES ( ?, ?, ?, ?, ? )" with ParamValues: 0=2, 1=\'hi\', 2=\'EUR\', 3=\'monthly\', 4=1] at ./test.pl line 20
- . Rollback failed: lost connection to storage at ./test.pl line 39
- '
- }, 'DBIx::Class::Exception' );
- creating rec 3
- DBIx::Class::Storage::DBI::_dbh_execute(): DBI Exception: DBD::mysql::st execute failed: MySQL server has gone away [for Statement "INSERT INTO cost ( amount, comment, currency, recurrence, resource_id) VALUES ( ?, ?, ?, ?, ? )" with ParamValues: 0=3, 1='hi', 2='EUR', 3='monthly', 4=1] at ./test.pl line 20
- ...and absolutely no records were created in the table.
Add Comment
Please, Sign In to add comment