Guest User

Untitled

a guest
Jul 15th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use threads;
  6. use DBI;
  7.  
  8. my ($firstUserConn, $firstSth, $firstResult);
  9. my ($secondUserConn, $secondSth, $secondUserResult);
  10.  
  11. # first user starts to connect
  12. $firstUserConn = DBI->connect('DBI:mysql:world', 'fatguy', 'ugalay') ||
  13. die "couldn't connect: $DBI::errstr";
  14.  
  15. # the first user is locking the tables for an update or whatever
  16. $firstSth = $firstUserConn->do('lock tables City write');
  17.  
  18. # another client needs to read some city stuff
  19. #$secondUserConn = DBI->connect('DBI:mysql:world', 'littlecoat', 'skinnbones') ||
  20. # die "couldn't connect: $DBI::errstr";
  21.  
  22. # actually, the second client needs to read a LOT of city stuff ;)
  23. # by doing this, i'm going to simulate a constant flow of traffic
  24. # hopefully, the site has an uptime expectancy of forever, so
  25. # we'll keep simulating page hits... FOREVER!
  26. # we'll do one page hit every second...
  27.  
  28. #while (1) {
  29. # $secondSth = $secondUserConn->prepare('select * from City');
  30. # $secondSth->execute();
  31. # while ($secondUserResult = $secondSth->fetchrow_hashref()) {
  32. # print "$secondUserResult->{'ID'} $secondUserResult->{'Name'} $secondUserResult->{'CountryCode'} $secondUserResult->{'District'} $secondUserResult->{'Population'}\n";
  33. # }
  34. # $secondSth->finish();
  35. # sleep(1);
  36. #}
  37.  
  38.  
  39. my @bigugly;
  40. for (my $i = 1; $i <= 100; $i++) {
  41. $bigugly[$i] = threads->new(\&makeQuery);
  42. $bigugly[$i]->detach;
  43. }
  44.  
  45. # okay this is to keep the connections open...
  46. while (1) {
  47. sleep(1);
  48. }
  49.  
  50.  
  51. # oh yeah, not that we'll actually get to this point, but firstuser will eventually finish his lock. ;)
  52. # it's true that i never did any sort of writes for him.
  53. # the only important thing for this exercise was the lock from the first user,
  54. # not necessarily the raw updates
  55. $firstUserConn->disconnect();
  56.  
  57. sub makeQuery {
  58. # don't care about the return val of the select
  59. # just creating a job to wait on the write lock
  60. $secondUserConn = DBI->connect('DBI:mysql:world', 'littlecoat', 'skinnbones') ||
  61. die "couldn't connect: $DBI::errstr";
  62. $secondUserConn->do('select * from City');
  63. $secondUserConn->disconnect();
  64.  
  65. }
Add Comment
Please, Sign In to add comment