Forum Moderators: coopster
First, I need to write some of the data to one of the tables so that I can extract (SELECT) the auto_incrementing primary key to use as a foreign key in the next two INSERTS.
Then I have to extract (SELECT) the auto_incrementing primary key from one of those tables to use as a foreign key in the last tables.
But if I get a failure part way through this operation, I could have a partial write and my data integrity is not just compromised... it's just plain bunged up!
What are some strategies that can be used to prevevnt this from happening?
Tom
I extract the primary key that will be used as a foreign key like this:
SELECT MAX(column_name) as for_key FROM table1 Then INSERT for_key into the second table.
But if two folks out there on the internet 'submit' almost simultaneously, the second submitter may end up with the first submitter's MAX(column_name) and my data will be further bunged-up.
Same question: how, in general, does one avoid this sort of unlikely, but possible, situation? Or does mySQL prevent this by only allowing one connection at a time, or something else like that?
Make sense?
Tom
(maybe this should be a separate topic...)
But your problem is dealing with partial data
So instead of focusing on what's happen after submission
Why do not check the form for correctness before submitting
Exactly. But it's not just data verification that could cause a problem. I've taken care of that. I'm thinking, if a network or server goes down during execution.
Or some problem I didn't find while testing. It wouldn't be the first time that's happened.
Thanks for responding.
Tom
‰Ž¶┐¿
check for the proper data type (no strings going into your integer fields), string length, valid date syntax, NULLness, etc.
if you're still worried about your database messing up halfway through a process, then check it all when it's done.
You'll still have all the incrementing ID numbers handy, right? So do a few SELECTs to make sure it all arrived safely, and if you find any of the records are missing, DELETE them all or deal with them in some other way.
good luck
If I understand you correctly, you are describing transactional processing (START TRANSACTION, COMMIT or ROLLBACK). Basically what happens is you START a TRANSACTION, run as many SQL statements as necessary, and if you reach the end successfully, COMMIT the entire bunch. If you don't COMMIT, you can explicitly set a ROLLBACK which puts everything back to where it was before you started and you would send the message stating so.
If your server is hit by a tornado half way through a process, it's not even going to be alive to do a final check. That's when a recent backup of your database will be handy.
Why not store a random checksum along with each step? the process could write an MD5 string like "d2jrl63k72dg3ds72ll6s3" at each step along with the data. Then when you reboot and clean up the debris, you can check if any of the records "don't match up"
and... install a UPS and some lightning rods. :)
Transactions and Atomic Operations [dev.mysql.com]
START TRANSACTION, COMMIT, and ROLLBACK Syntax [dev.mysql.com]
As the link above states, MySQL Server (version 3.23-max and all versions 4.0 and above) supports transactions with the InnoDB and BDB transactional storage engines. InnoDB provides full ACID compliance. However, you can simulate transactions with non-transactional (ISAM, MyISAM) tables. Keep these functions in an abstraction layer and when it's time for you to move to transactional tables, your coding will be a breeze.
trying to grasp the abstraction layer concept
thanks
Henry
A small example, (Disclaimer: nowhere near complete), but should give you the concept...
To simulate transactions, you might use table-locking and your own error-handling, so you create a script that will use dynamic variables to accomplish the task at hand, keep the script/function separate so that you can use it from all over the place in different scripts as required.
$sql = "LOCK TABLES $table1 $action1, $table2 $action2";As I stated, this is nowhere near complete, but hopefully will give you an idea of where you could take things...
// of course, this next line would be another asbtraction layer,
// like the dba functions you mentioned
mysql_query($sql);
$sql = "INSERT INTO $table1 ($columns1) VALUES ($values1)";
mysql_query($sql);
$sql = "INSERT INTO $table2 ($columns2) VALUES (LAST_INSERT_ID(), $values2)";
mysql_query($sql);
$sql = "UNLOCK TABLES";
mysql_query($sql);
I need to get my head around how (SQLServer) Stored Procedures relate to transactional or atomic operations. Perhaps they are not really related, perhaps they're synonymous. Perhaps it's irrelevant.
But the COMMITTING and ROLLING BACK may be what I'm needing. It seems there's a lot more meat to mySQL than I thought. That's good.
Thanks again. And thanks for the welcome coopster. It's always nice to find a group who's on the ball and willing to share knowledge.
Tom