If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > transaction in stored procedure

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-10-09, 16:52
ozzii ozzii is offline
Registered User
 
Join Date: Mar 2007
Posts: 194
transaction in stored procedure

I am trying to write a stored procedure , which uses transactions

the following is a sample code.

Code:
create procedure test_proc(IN id int,IN name varchar(20) )
begin
start transaction;

--insert into table1 some data and get last insert id;

--insert into table2 some data using last insert id;


commit;
end;
however am not sure of the correct syntax. In the above procedure if an error occurred would the procedure automatically rollback? If it failed would it generate an error message and if so how would you report that in php? If it was successful again how would you know this. Basically I need some way of knowing if the procedure was successful or not in my php code but at the same time need to know if the procedure would automatically rollback if all or any insert failed.
Reply With Quote
  #2 (permalink)  
Old 05-14-09, 23:14
leonel.machava leonel.machava is offline
Registered User
 
Join Date: May 2009
Posts: 5
Hi ozzii!

When an error occurs mysql doesn't automatically rollback. You must create handlers (see MySQL :: MySQL 5.0 Reference Manual :: 12.8.4.2 DECLARE for Handlers) that are executed when an error occurs. I do that in listing 1.
As you can see in the stored procedure I added the parameter ret. This parameter holds the return code of the stored procedure (1-sucess, 0-failure).

In listing 2, I present a snippet of PHP code that calls the stored procedure and checks if it returned successfully or not.

Listing 1. STORED PROCEDURE:

create procedure sp_answer(IN name VARCHAR(20),OUT ret TINYINT)
begin
declare exit handler for not found rollback;
declare exit handler for sqlwarning rollback;
declare exit handler for sqlexception rollback;

set ret=0;
start transaction

-- insert into ...
-- update ...
-- any other DDL operation

commit;

set ret = 1;
end

Listing 2. PHP CODE:

/* MySQL connection code here. */

$res = mysql_query("CALL sp_answer(@res)");

/* TO CHECK PROCEDURE RETURN STATUS: */
$res = mysql_query("SELECT @res");
$row = mysql_fetch_array($res);
$ret = (int)$row[0];
if( $ret ) {
print "sucess!";
}
else {
print "failed!";
}

Let me know if you have any doubt!!!

Peace!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On