[oracle@node01 perl]$ cat test1.pl
use DBI;
use Encode;
use Data::Dumper;
my $dbName = 'serv';
my $dbUser = 'system';
my $dbUserPass = 'oracle';
$dbh = DBI->connect("dbi:Oracle://1.168.137.2:1521/serv", 'system', 'oracle') or die "can't connect to database ";;
[oracle@node01 perl]$ time perl test1.pl
DBI connect('//1.168.137.2:1521/serv','system',...) failed: ORA-12170: TNS: 连接超时 (DBD ERROR: OCIServerAttach) at test1.pl line 7
can't connect to database at test1.pl line 7.
real 1m0.132s
user 0m0.017s
sys 0m0.047s
[oracle@node01 perl]$ cat test2.pl
use DBI;
use Encode;
use Data::Dumper;
use Sys::SigAction ;
use Sys::SigAction qw( set_sig_handler );
eval {
my $h = set_sig_handler( 'ALRM' ,sub { die "connect timeout for 10
" ; } ); #数据库连接超时后的返回结果
alarm(10); #设置为5秒超时
$dbh = DBI->connect("dbi:Oracle://1.168.137.2:1521/serv", 'system', 'oracle') or die "can't connect to database ";;
alarm(0);
};
alarm(0);
if ( $@ ) { print $@."
"; } #数据库连接返回失败后的返回结果
##############################
[oracle@node01 perl]$ time perl test2.pl
connect timeout for 10
real 0m10.058s
user 0m0.032s
sys 0m0.058s