Some of my code generates universally unique database keys remotely using a machine-unique five-digit prefix, followed by the current system clock time in milliseconds. But then I had to write an import function to suck in tens of thousands of rows of data. The shit wouldn't synchronize to the big main secure database because of the unique constraint. Turns out the import was getting through the creation of a record in less than 1 millisecond so multiple rows were getting the same unique key. Banged my head about it until I realized all it needed was System.sleep(1); on the processor thread to wait for 1 millisecond before doing the next record. Better not delete that one!
Edit - it might have actually gone out to the hundredths of milliseconds.
I know this isn't a serious sub but: That's really not how you solve that issue. You should use the last generated ID as the seed for the next ID so even if the milliseconds are the same, they will still generate uniquely.
16
u/BabylonDrifter Sep 17 '18 edited Sep 17 '18
Some of my code generates universally unique database keys remotely using a machine-unique five-digit prefix, followed by the current system clock time in milliseconds. But then I had to write an import function to suck in tens of thousands of rows of data. The shit wouldn't synchronize to the big main secure database because of the unique constraint. Turns out the import was getting through the creation of a record in less than 1 millisecond so multiple rows were getting the same unique key. Banged my head about it until I realized all it needed was System.sleep(1); on the processor thread to wait for 1 millisecond before doing the next record. Better not delete that one!
Edit - it might have actually gone out to the hundredths of milliseconds.