The application dies when insertion fails.

From: Antai Ning <aning(at)jetnet(dot)ca>
To: pgsql-general(at)postgresql(dot)org
Subject: The application dies when insertion fails.
Date: 2002-05-30 18:11:09
Message-ID: F72CDFC92CAA204A9BE73476EED3821D015E5F@JETMAIL
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="MS Exchange Server version 5.5.2653.12">
<TITLE>The application dies when insertion fails.</TITLE>
</HEAD>
<BODY>

<P><FONT SIZE=2>Hi Gurus,</FONT>
</P>

<P><FONT SIZE=2>This has been bothered me for a couple of days. I wrote a postgres db application in perl which parses a syslog file and inserts some data into a table. I put the actual insertion in a eval block, so the application won't die when insertion fails. But it doesn't work as I expected. When it encounter a &quot;DBD::Pg::st execute failed: ERROR:&nbsp; Bad timestamp external representation 'Jan 11 18:05:50&nbsp;&nbsp; at (eval 9) line 426, &lt;MSGS&gt; line 1.&quot; error, the whole program die.</FONT></P>

<P><FONT SIZE=2>I'm using DBD::Pg intereface, postgres 7.1.3. Here is the codes:</FONT>
</P>

<P><FONT SIZE=2>-----------------------------------------------------------------------</FONT>
<BR><FONT SIZE=2>my $dbh = DBI-&gt;connect(&quot;dbi:Pg:dbname=$dbname&quot;,$dbuser,$dbpass,{AutoCommit =&gt; 0,RaiseError =&gt; 1,PrintError =&gt; 0}) || die &quot;Unable to connect to $dbname on $dbhost [$DBI::errstr]\n&quot;;</FONT></P>

<P><FONT SIZE=2>while(1)</FONT>
<BR><FONT SIZE=2>{</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2># reading from MSGS and parsing here</FONT>
</P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>$sth = $dbh-&gt;prepare(&quot;insert into http_request</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>(INTERFACE_ID,INTERFACE_DIR,REQUEST_TIME,SRC,URL,result_id)</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; values(?, ?, ?, ?, ?, ?)&quot;);</FONT>
</P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>eval {</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $sth-&gt;execute($ifid,$idir,$timestamp,$userip,$destip,$rid);</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $sth-&gt;finish;</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $dbh-&gt;commit;</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>};</FONT>
</P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>if($@)</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>{</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &quot;Insert failed\n&quot;;</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &quot;$@&quot;;</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>}</FONT>
<BR><FONT SIZE=2>}</FONT>
</P>

<P><FONT SIZE=2>---------------------------------------------------------------------------</FONT>
</P>

<P><FONT SIZE=2>I noticed that the timestamp is not valid, but the excution should continue. Strange thing is that it didn't die right away. After print&quot;$@&quot; prints out the error &quot;DBD::Pg::st execute failed: ERROR:&nbsp; Bad timestamp external representation 'Jan 11 18:05:50&nbsp;&nbsp; at (eval 9) line 426, &lt;MSGS&gt; line 1.&quot; it dies.</FONT></P>

<P><FONT SIZE=2>So what do I miss here?</FONT>
</P>

<P><FONT SIZE=2>Thanks in advance!</FONT>
</P>
<BR>

<P><FONT SIZE=2>Andy</FONT>
</P>

</BODY>
</HTML>

Attachment Content-Type Size
unknown_filename text/html 3.6 KB

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2002-05-30 18:54:24 Re: horrendous query challenge :-)
Previous Message Darren Ferguson 2002-05-30 18:05:04 Re: connection refused problem