Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- use threads;
- use DBI;
- my ($firstUserConn, $firstSth, $firstResult);
- my ($secondUserConn, $secondSth, $secondUserResult);
- # first user starts to connect
- $firstUserConn = DBI->connect('DBI:mysql:world', 'fatguy', 'ugalay') ||
- die "couldn't connect: $DBI::errstr";
- # the first user is locking the tables for an update or whatever
- $firstSth = $firstUserConn->do('lock tables City write');
- # another client needs to read some city stuff
- #$secondUserConn = DBI->connect('DBI:mysql:world', 'littlecoat', 'skinnbones') ||
- # die "couldn't connect: $DBI::errstr";
- # actually, the second client needs to read a LOT of city stuff ;)
- # by doing this, i'm going to simulate a constant flow of traffic
- # hopefully, the site has an uptime expectancy of forever, so
- # we'll keep simulating page hits... FOREVER!
- # we'll do one page hit every second...
- #while (1) {
- # $secondSth = $secondUserConn->prepare('select * from City');
- # $secondSth->execute();
- # while ($secondUserResult = $secondSth->fetchrow_hashref()) {
- # print "$secondUserResult->{'ID'} $secondUserResult->{'Name'} $secondUserResult->{'CountryCode'} $secondUserResult->{'District'} $secondUserResult->{'Population'}\n";
- # }
- # $secondSth->finish();
- # sleep(1);
- #}
- my @bigugly;
- for (my $i = 1; $i <= 100; $i++) {
- $bigugly[$i] = threads->new(\&makeQuery);
- $bigugly[$i]->detach;
- }
- # okay this is to keep the connections open...
- while (1) {
- sleep(1);
- }
- # oh yeah, not that we'll actually get to this point, but firstuser will eventually finish his lock. ;)
- # it's true that i never did any sort of writes for him.
- # the only important thing for this exercise was the lock from the first user,
- # not necessarily the raw updates
- $firstUserConn->disconnect();
- sub makeQuery {
- # don't care about the return val of the select
- # just creating a job to wait on the write lock
- $secondUserConn = DBI->connect('DBI:mysql:world', 'littlecoat', 'skinnbones') ||
- die "couldn't connect: $DBI::errstr";
- $secondUserConn->do('select * from City');
- $secondUserConn->disconnect();
- }
Add Comment
Please, Sign In to add comment