I have two batch classes that run concurrently. Let's call them Job A and Job B.
Job A inserts records in object A. And Job B in object B. Object B has a lookup to Object A.
Because this lookup is mandatory, and because we do not know whether we'll have the record in Object A populated before the record in Object B is saved, we run a quick check and create a "shell" Object A record (Name and ExternalID are the only two fields populated), expecting that Job A will eventually upsert the records and fill in the blanks.
Problem is, we're getting an error when saving Object A records:
duplicate value found. I've been able to diagnose that the only records that fail with this error are those being created by the current batch in Job B.. That is, those records processed in previous batches of Job B get upserted properly. Obviously, for those Object B records created after this failure there is no need for the "shell" records, therefore there is no fault.
The error is consistently on Job A, even though both jobs do upserts. This makes sense because batches of Job B upsert the shell records at the very beginning, whereas in Job A we have to wait until the end of the batch...
This sounds like a concurrency issue... but I can't figure out how to get around it. Help!
PS: There are good business reasons for the parallel process, we already know this is not a problem if we process Job B after Job A finishes.