views:

40

answers:

3

Hi everyone,

I make bigger inserts consisting of a couple of thousand rows in my current web app and I would like to make sure that no one can do anything but read the table, until the inserts have been done.

What is the best way to do this while keeping the read availability open for normal, non-admin users?

Thanks!

+1  A: 

Why not just do the insert in one big transaction? That would kind of prevent the need for any locking.

webdestroya
+2  A: 

Since you're only inserting (not updating) this shouldn't be a problem. Just insert the records in a single transaction.

InnoDB supports row level locking if I recall correctly so even updates shouldn't be a problem.

ChristopheD
+1  A: 

You may also need to change default isolation level:

 set transaction isolation level serializable;
 start transaction;
 // insert data
 commit
a1ex07
Hi A1ex07. Thanks a lot. Is this a temporarily solution that needs to be called on each query (which would be awesome) or is it permanent? Thanks!
Industrial
It depends on business logic of your application. Serializable isolation level guarantees that no other transactions can change data that may somehow affect the current transaction. For instance, after executing SELECT count(*) FROM table1 WHERE field1 < 10, other transactions cannot insert/delete/update records from table1 if it changes the result of the query. So if your INSERTS don't have to be executed in particular order and each next query doesn't depend on previous, you can run each query inside a separate transaction...
a1ex07