-
Notifications
You must be signed in to change notification settings - Fork 0
/
perl-connection-test.pl
68 lines (49 loc) · 1.69 KB
/
perl-connection-test.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/perl
use strict;
use DBI;
my $dbh = undef;
my $stm = undef;
sub printLog {
printf("Log: %s\n", shift);
}
sub connectToDatabase {
$dbh = DBI->connect('DBI:mysql:database=myapplication;host=192.168.33.2',
'auser', 'auser', { RaiseError => 1, mysql_auto_reconnect => 1})
or die('Was not able to connect to the database!');
printLog('Connected to the database!');
}
sub createTable {
$dbh->do('DROP TABLE IF EXISTS connectionTest');
printLog('Dropped database connectionTest');
$dbh->do('CREATE TABLE connectionTest (id INT UNSIGNED NOT NULL AUTO_INCREMENT, message VARCHAR(255) NOT NULL, PRIMARY KEY (id))');
printLog('Recreated table connectionTest');
}
sub prepareStatement {
$stm = $dbh->prepare('INSERT INTO connectionTest (message) VALUES(?)');
printLog('Statement prepared!');
}
sub insertRow {
$stm->execute(sprintf('I was inserted %s the connection was lost', shift));
printLog(sprintf('%s statement was executed!', shift));
}
sub insertFirstRow {
insertRow('before', 'First');
}
sub insertSecondRow {
insertRow('after', 'Second');
}
sub waitForUserToKillTheServer {
print('Now kill the server with the floating IP! Hit enter afterwards');
<STDIN>;
}
sub main {
printLog("Please create the database and the user first! See README for details.\n");
connectToDatabase();
createTable();
prepareStatement();
insertFirstRow();
waitForUserToKillTheServer();
insertSecondRow();
printLog('If you see this message your application survived the outage without only one additional line of code. I just turned on the mysql_auto_reconnect feature! Grab a beer now! You have earned it!');
}
main();