Skip to content

Instantly share code, notes, and snippets.

@akarelas-pt
Created November 24, 2017 10:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akarelas-pt/eea93b8146789acb69e7d07640ae6768 to your computer and use it in GitHub Desktop.
Save akarelas-pt/eea93b8146789acb69e7d07640ae6768 to your computer and use it in GitHub Desktop.
repeated dropped connection causes DBIC to fail
#!/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.
@akarelas-pt
Copy link
Author

I would expect record 3 to be created.

Shouldn't it?

Isn't this a bug in DBIx::Class?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment