{"id":204,"date":"2014-06-27T00:53:44","date_gmt":"2014-06-26T21:53:44","guid":{"rendered":"http:\/\/meekconsulting.com\/?p=204"},"modified":"2022-01-08T16:26:32","modified_gmt":"2022-01-08T13:26:32","slug":"handling-errors-in-mysql-procedures-with-rollback","status":"publish","type":"post","link":"https:\/\/meekconsulting.com\/?p=204","title":{"rendered":"Handling Errors in MYSQL  Procedures with Rollback"},"content":{"rendered":"<p>f you have mysql stored procedure that changes database tables \u2014 you probably want to enclose the whole procedure in a single transaction and add error handling to roll back the work if anything fails.<\/p>\n<p>Here is how to do it:<br \/>\nKey points:<br \/>\nUse START TRANSACTION at top after BEGIN<br \/>\nUse COMMIT at end<\/p>\n<p>Turn on Transactions with START TRANSACTION after the BEGIN<\/p>\n<p>And end the transaction with COMMIT at the end of your procedure<\/p>\n<p>Add an error handling block that calls ROLLBACK work if there is any SQL EXCEPTION.<\/p>\n<p>This is example SENDs a custom crafted ERROR message ( similar to RAISE_APPLICATION_ERROR in ORACLE ) which our PHP code recognizes and displays \/ logs.<\/p>\n<blockquote><p>Here is the magic:<br \/>\nCREATE<br \/>\nDEFINER = CURRENT_USER<br \/>\nPROCEDURE bocs_create_budget(psource_bud_id INT(11), ptitle VARCHAR(200), pfy INT(4), pout_years INT(11))<br \/>\nBEGIN<\/p>\n<p>DECLARE new_bud_id INT(11);<\/p>\n<p><strong>DECLARE EXIT HANDLER FOR SQLEXCEPTION<\/strong><br \/>\n<strong> BEGIN<\/strong><br \/>\n<strong> GET DIAGNOSTICS CONDITION 1 @sqlstate = RETURNED_SQLSTATE,<\/strong><br \/>\n<strong> @errno = MYSQL_ERRNO, @text = MESSAGE_TEXT;<\/strong><br \/>\n<strong> SET @full_error = CONCAT(\u201cERROR \u201c, @errno, \u201d (\u201c, @sqlstate, \u201c): \u201c, @text);<\/strong><br \/>\n<strong> ROLLBACK;<\/strong><br \/>\n<strong> SIGNAL SQLSTATE \u201845000\u2019<\/strong><br \/>\n<strong> SET MESSAGE_TEXT = @full_error, MYSQL_ERRNO = 1001;<\/strong><br \/>\n<strong> END;<\/strong><\/p>\n<p><strong>START TRANSACTION;<\/strong><\/p>\n<p>SET @TRIGGER_CHECKS = FALSE;<\/p>\n<p>\u2014 Procedure work that may raise errors is done here<\/p>\n<p>COMMIT;<\/p>\n<p>SET @TRIGGER_CHECKS = TRUE;<br \/>\nEND $$<br \/>\nDELIMITER ;<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>f you have mysql stored procedure that changes database tables \u2014 you probably want to enclose the whole procedure in a single transaction and add error handling to roll back the work if anything fails. Here is how to do &hellip; <a href=\"https:\/\/meekconsulting.com\/?p=204\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/meekconsulting.com\/index.php?rest_route=\/wp\/v2\/posts\/204"}],"collection":[{"href":"https:\/\/meekconsulting.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meekconsulting.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meekconsulting.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/meekconsulting.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=204"}],"version-history":[{"count":2,"href":"https:\/\/meekconsulting.com\/index.php?rest_route=\/wp\/v2\/posts\/204\/revisions"}],"predecessor-version":[{"id":207,"href":"https:\/\/meekconsulting.com\/index.php?rest_route=\/wp\/v2\/posts\/204\/revisions\/207"}],"wp:attachment":[{"href":"https:\/\/meekconsulting.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meekconsulting.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meekconsulting.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}