tags:

views:

45

answers:

1

Hello Masters!

I have an user registration procdedure, with this steps:

If user send the registraion form then:

 1. step: run a QUERY
 2. step: run another QUERY
 3. make a directory1 on the server
 4. make a sub directory2 into the directory1
 5. make another sub directory3 into the directory1
 6. upload an image into the sub directory2

So this is work well. But im afraid if something happen while this procedure is runnin, so if the procedure interrupted on 4. step, then i have some unnecessary row in my table bacause of step 1.; 2.

So i think i have to use mysql transaction handle, but i don't know how. I think somethink like this:

     START TRANSACTION (what is the exact format?)
     1. step: run a QUERY
     2. step: run another QUERY
     3. make a directory1 on the server
     4. make a sub directory2 into the directory1
     5. make another sub directory3 into the directory1
     6. upload an image into the sub directory2
     COMMIT (if all step ok. but how can i check this?)
     ROLLBACK (if something wrong; but how can i check this?)

 I this transaction handle not handle the FILE transactions, so i ROLLBACK function is called, then i have to delete manually the directorys if created already?!

Sorry, two more question:
  • If the procedure interrupted, for example in step 2, and ROLLBACK is called, then the sciprt will stop or start from the end? Or i need to write a callback funciton something like this: (is this how i have to do?)

    function begin() { @mysql_query("BEGIN");}
    function commit(){ @mysql_query("COMMIT");}
    function rollback()
    {
        @mysql_query("ROLLBACK");
        echo "oops";
        exit();
    }
    $inster_query="insert into....
    begin();
    $result_insert = @mysql_query($inster_query);
    if(!$result_insert)
    {
      rollback(); 
    }
    $update_query="update....
    $result_up = @mysql_query($update_query);
    if(!$result_up)
    {
      rollback(); 
    }
    .
    .
    commit(); 
    
  • how can i test this if its work or not? Thank you.

+1  A: 

START TRANSACTION - see manual

  1. step: run a QUERY. If it fails - rollback.
  2. step: run another QUERY. If it fails - rollback.
  3. make a directory1 on the server. If it fails - rollback.
  4. make a sub directory2 into the directory1. If it fails, delete directory1 and rollback.
  5. make another sub directory3 into the directory1. If it fails, delete subdirectory2 and directory 1 and rollback.
  6. upload an image into the sub directory2. If it fails, delete three previous directories and rollback. COMMIT

From development perspective, you need to create Undo stack and push there objects, that are capable of undoing the work. In case something fails, you need to ->Execute() the entire undo stack. I guess you know how to check if directory creation/file upload failed?

FractalizeR
ok, so i need to check it step by step not just the end; this undo stack think..im not sure i understand, but i try to do as u suggested;
Holian
Yes, check step by step. Something about undo/redo stacks: http://bytes.com/topic/c-sharp/answers/235457-how-do-we-normally-implement-undo-redo-function
FractalizeR
i edited my comment, add two more question. if u have a little time pls look at
Holian
btw: If STEP 1(insert) is OK, but STEP 2(update) NOT, then STEP 2 will excetuce rollback(), and then will run rollback() in STEP 1 too? (if(!$result_insert) {rollback();} if(!$result_update) {rollback();})
Holian
When ROLLBACK is called, all queries, that were executed after "START TRANSACTION" are automatically undone.
FractalizeR