Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Capture alarm signal

by vinoth.ree (Parson)
on Sep 16, 2010 at 11:12 UTC ( #860338=perlquestion: print w/ replies, xml ) Need Help??
vinoth.ree has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks

I have written a sample perl program to connect with the database within the time limit of 10 seconds, If is not get connected it should return connection timeout error message.

For this I am using the alarm signal, as below

test.pl
use DbConn; use Data::Dumper; eval{ local $SIG{ALRM} = sub { print "Alarm Captured\n" ; exit ; } ; my $Sleep_Obj= DbConn->New(); alarm 10 ; $Sleep_Obj->connection('db_name','hostip','username','password'); alarm 0; }; if ($@) { print "Timed out.\n"; } else { print "Connected Successfully\ +n"; }

The above code includes the Dbconn.pm module and connecting with the database with the given ip address, username and password, there may be a chance of database server down, at that time I should be getting the connection timeout, but it never comes out even after reaching the time out. Any possible way to capture the alarm signal on timeout.

DbConn.pm
package DbConn; use DBI; use Data::Dumper; sub New { my ($Class)=shift; my $Self = {}; bless($Self, $Class); return $Self; } sub connection { shift @_; my ($DBname, $Host, $User_Name, $Password)=@_; my $dbh = DBI->connect("dbi:Pg:dbname=$DBname;host=$Host","$User_ +Name","$Password",\%attr ) || die $DBI::errstr; return $dbh; #return the database handle } 1;
Updated

Comment on Capture alarm signal
Select or Download Code
Re: Capture alarm signal
by BrowserUk (Pope) on Sep 16, 2010 at 11:16 UTC
Re: Capture alarm signal
by salva (Monsignor) on Sep 16, 2010 at 11:28 UTC
    Postgresql and maybe other DBD modules already support setting timeouts for establishing the connection. They could be using the process alarm timer internally, effectively disabling the one set on your code.
Re: Capture alarm signal
by Limbic~Region (Chancellor) on Sep 16, 2010 at 13:30 UTC
    ree,
    Amazing. Yesterday in the CB, I was asking about options of limiting the scope of unsafe signals. When prompted "why would you want to do that?", I indicated that I was trying to get an exclusive lock on a table using DBD::DB2 but instead of failing instantly it was going into a lock-wait state. After discovering alarm didn't work as desired with safe signals, I tested and found unsafe signals to work. It turns out there is a way to limit how long you are willing to wait for a lock at the session level. The setting is not exposed through DBD::DB2 but rather is done using $dbh->do($statement).

    It was pointed out to me by Corion as it has been to you here that this is likely because the DB driver is also using alarm to handle the built-in time out. Assuming we are right, you have two options. The first is to use unsafe signals. The second is to discover how to time out your connection natively and trap it in a eval as I did.

    Cheers - L~R

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://860338]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (9)
As of 2014-07-22 19:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (126 votes), past polls