|
|
Sometimes you want to insert data in different tables, but you want to
insert them as a group of data. In other words, the integrity of
the group insert is dependent on each individual insert working.
If any of them fail, you want to erase what you did and start over from
the beginning. This is a Transaction.
-
You can make a transaction with the assistance of COMMIT and ROLLBACK
statements.
-
These are database terms. A COMMIT indicates that you are
pleased with the transaction and wish to make it final. A ROLLBACK
states that something went wrong, and you wish to go back to the initial
state.
-
By default, inserts and updates you make are auto-committed; that is, they
are committed when executed.
-
But, if you want to run these as a transaction, you need to turn autocommit
off and send the COMMIT message itself.
-
If something goes wrong before the COMMIT message is reached,
an exeption will usually be thrown. A SQLException, for
instance. Then, in the exception handler, you can issue a ROLLBACK
statement.
-
From this we learn two things: 1) try-catch blocks do have significant
value, and 2) Wal-Mart didn't invent the word ROLLBACK.
The Details
|