{"id":7,"date":"2008-04-08T16:32:09","date_gmt":"2008-04-08T16:32:09","guid":{"rendered":"http:\/\/www.bacondrivencoding.com\/2008\/2008\/04\/debugging-adventures-dbd-sybase\/"},"modified":"2025-08-02T14:48:00","modified_gmt":"2025-08-02T14:48:00","slug":"debugging-adventures-dbd-sybase","status":"publish","type":"post","link":"https:\/\/www.bacondrivencoding.com\/2008\/2008\/04\/debugging-adventures-dbd-sybase\/","title":{"rendered":"Debugging adventures with DBD::Sybase"},"content":{"rendered":"<p>So, I recently debugged an error in some code I inherited. It was a relatively bizarre corner case that I thought might be interested.<\/p>\n<p>The code is Perl running on windows, accessing a sybase database using DBI and DBD::Sybase.<\/p>\n<p>This was the basic structure of the code:<\/p>\n<pre><code> sub process_record {\r\n\tmy $record = shift;\r\n\r\n\tif (is_record_in_db($record)) {\r\n\t\tif (mark_existing_record_deleted($record)) {\r\n\t\t\tinsert_record($record);\r\n\t\t}\r\n\t} else {\r\n\t\tinsert_record($record);\r\n\t}\r\n}<\/code><\/pre>\n<p>There was some other stuff in there too, but it doesn&#8217;t matter for this story.<\/p>\n<p>This code had been in production for about a year, and it&#8217;s been working fine. Suddenly we start getting this error:<\/p>\n<pre>Panic: Can't have multiple statement handles on a single database handle when AutoCommit is OFF<\/pre>\n<p>It also gives a line number from the bowels of DBD::Sybase. I try to get a full stack trace by running it under -MCarp=verbose . But, DBD::Sybase doesn&#8217;t croak, it dies, so I can&#8217;t see which call of mine is killing it. I don&#8217;t feel like hitting the debugger yet, so I think some more.<\/p>\n<p>Now, this error comes only after we&#8217;ve processed 5000 or so records, so I take a look to see what&#8217;s different about the record where it fails, and I notice that it takes the path which calls mark_existing_record_deleted, while none of the others do. (Inserts heavily outnumber updates on this application.)<\/p>\n<p>(Note: all of the called routines access the database.) DBD::Sybase doesn&#8217;t allow you to have multiple statement handles. So I look in the code and see that, sure enough, mark_existing_record_deleted doesn&#8217;t have a call to $sth-&gt;finish . But neither does is_record_in_db! Now I&#8217;m wondering, &#8220;how does this thing work at all?&#8221;<\/p>\n<p>The answer is, that DBD::Sybase automatically finishes the handle for you when you read past the last row. So when there&#8217;s no data in the database, the query in is_record_in_db returns no rows. One fetch is &#8220;past the end,&#8221; so the handle gets closed. But when there is data, the query handle is open, causing the application to die when you try to open another handle in mark_existing_record_deleted.<\/p>\n<p>So, watch out for that when you write code against Sybase.<\/p>\n<link rel=\"stylesheet\" href=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/highlight.js\/11.11.1\/styles\/default.min.css\">\n<script src=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/highlight.js\/11.11.1\/highlight.min.js\"><\/script><br \/>\n<script>hljs.highlightAll();<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>So, I recently debugged an error in some code I inherited. It was a relatively bizarre corner case that I thought might be interested. The code is Perl running on windows, accessing a sybase database using DBI and DBD::Sybase. This was the basic structure of the code: sub process_record { my $record = shift; if [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-7","post","type-post","status-publish","format-standard","hentry","category-code"],"_links":{"self":[{"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/posts\/7","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/comments?post=7"}],"version-history":[{"count":3,"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/posts\/7\/revisions"}],"predecessor-version":[{"id":93,"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/posts\/7\/revisions\/93"}],"wp:attachment":[{"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/media?parent=7"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/categories?post=7"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/tags?post=7"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}