php - I'm hitting a race condition in my Laravel application when trying to conditionally INSERT or UPDATE, any suggestions...? -
my users need able upload files site, i've implemented file uploader widget on frontend. allows multiple uploads @ once, , each upload triggers code 1 file @ time save file db.
the problem files need stored array in single row in database (i know, know... legacy reasons).
in english pseudocode, here's what's happening:
- laravel sees new file has been uploaded
- laravel checks whether or not files (at all) have been uploaded entity
- no files have been uploaded yet? create new record store file.
- there files entity? update existing record add file array.
the problem when multiple files uploaded @ once in quick succession first time, laravel has entered first file in database moments after second file has conducted it's check see if files exist. end duplicate rows, rather updating them in single record.
if upload 5 files @ once, typically i'll 4 rows in database - 3 single entries , 1 double-entry, managed catch in time.
any practical ways around problem? know should using many-to-one database schema here, i've simplified complex situation brevity!
this laravel 5.2 using mysql innodb database.
plan a: when see 1 new file, wait little while. 'all' files , deal them.
plan b: store timestamp record. when file noticed, see if there existing record 'recent' timestamp. if so, assume part of same 'group'.
both plans occasionally have hiccups -- because of vague definition of "at once".
perhaps have problem: file half uploaded when grab it, , incomplete file?
the 'real' answer have 'message' uploaded when finished. then, don't start processing until see it. (i realize not practical.)
Comments
Post a Comment