Guest User

Untitled

a guest
Nov 25th, 2017
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. #!/usr/bin/env perl
  2.  
  3. use FindBin '$Bin';
  4. use lib "$Bin/local/lib/perl5", "$Bin/lib";
  5. use v5.16;
  6. use warnings;
  7.  
  8. use My::Schema;
  9.  
  10. use Data::Dumper;
  11.  
  12. my $schema = My::Schema->my_connect;
  13.  
  14. my $COSTS = $schema->resultset('Cost');
  15.  
  16. sub create {
  17. my ($amount) = @_;
  18. $amount //= 0;
  19.  
  20. $COSTS->create({
  21. resource_id => 1,
  22. amount => $amount,
  23. currency => 'EUR',
  24. recurrence => 'monthly',
  25. comment => 'hi',
  26. });
  27. }
  28.  
  29. eval {
  30. $schema->txn_do(sub {
  31. say 'creating record 1';
  32. create(1);
  33. say 'created rec 1';
  34. say 'disconnecting';
  35. $schema->storage->dbh->disconnect;
  36. say 'creating record 2';
  37. create(2);
  38. say 'created rec 2';
  39. });
  40. };
  41.  
  42. if (my $e = $@) {
  43. say 'caught error';
  44. say Dumper($e);
  45. }
  46.  
  47. say 'creating rec 3';
  48. create(3);
  49.  
  50. __END__
  51.  
  52. Output follows:
  53.  
  54. creating record 1
  55. created rec 1
  56. disconnecting
  57. creating record 2
  58. creating record 1
  59. created rec 1
  60. disconnecting
  61. creating record 2
  62. caught error
  63. $VAR1 = bless( {
  64. '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
  65. . Rollback failed: lost connection to storage at ./test.pl line 39
  66. '
  67. }, 'DBIx::Class::Exception' );
  68.  
  69. creating rec 3
  70. 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
  71.  
  72.  
  73. ...and absolutely no records were created in the table.
Add Comment
Please, Sign In to add comment