sql - Reserving mySQL auto-incremented IDs? -


we want obtain auto-increment id mysql without storing until other non-mysql related processes completed, entry not stored if exception or application crash happens. need use id key other processes. in essence want “reserve” auto-increment , insert rows mysql last step. don’t want insert row until know entire process has completed successfully.

is possible sort of auto-increment reservation in mysql?

note: know sql transactions. our process contains non-sql stuff need happen outside of db. these process may take few mins several hours. don't want other process using same auto-increment id. why want "reserve" auto-increment id without inserting data db. –

the way generate auto-increment value attempt insert. can roll transaction, , still read id generated. in mysql 5.1 , later, default behavior auto-increment values aren't "returned" stack when roll back.

start transaction; insert mytable () values (); rollback; select last_insert_id() @my_ai_value; 

now can sure no other transaction try use value, can use in external processes, , insert value manually uses id value (when insert specific id value, mysql not generate new value).


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -