my $dbh; RECONNECT: eval { my $dsn = "DBI:$driver:database=$database;host=$hostname;mysql_connect_timeout=10"; $dbh = DBI->connect($dsn, $username, $password, { RaiseError => 1, PrintError => 0, AutoCommit => 1 }); }; if( $@ ) { goto RECONNECT; } eval { # Setup ALRM handler signal local $SIG{ALRM} = sub { die "timeout\n" }; alarm( 60 ); $dbh->do("INSERT INTO table_name VALUES ( data, ... )"); alarm( 0 ); }; if( $@ ) { if( $@ =~ /timeout/ ) { print "Timed out\n"; } else { alarm( 0 ); } }