0

I've been reading up on transactions and table locking for a while and I am still unclear on something and hoping to find clarification here:

So the situation is that I need to insert blocks of entries (say 10 at a time) with each one containing a count of the number of entries before the block was inserted (so rows 51-60 would contain an entry of 50)

Would wrapping the entire process in a transaction be sufficient to guarantee this count in a high IO database?

e.g.

  1. Start transaction
  2. select (Count *) on table_a
  3. insert block of rows on table_a and include count from select
  4. End transaction

In the above situation can I guarantee that no rows are inserted between the count and the last insert? (Mysql/InnoDB).

Thanks in advance for your help.

SwiftD
  • 5,769
  • 6
  • 43
  • 67
  • You can do this with try..catch ( transaction ), [mypost](http://stackoverflow.com/questions/21749641/php-mysqli-transactions) , also each query you can check with [condition](http://stackoverflow.com/questions/21776040/condition-statement-php-mysqli) – user3209031 Apr 11 '14 at 12:18
  • I had a look through those posts and I'm still not clear. Are you saying that essentially I need to run an additional select to verify the counts after the inserts, throw an error if wrong, catch it and rollback. Do I loop this and try again? - I dont want to end with a failed query. Could you present this as an answer for clarity? – SwiftD Apr 11 '14 at 12:32
  • [Check out this](http://w3schools.invisionzone.com/index.php?showtopic=49574&hl=) and its working for me – user3209031 Apr 11 '14 at 12:38

0 Answers0