1

Scenario: I've created a Schedule Apex Job to run an 'X' class after every one hour. I will be running it through Developer Console using 'Anonymouse Execute Window'. The 'X' class inserts a large volume of data.

Suppose, within one hour, all the data weren't inserted, then what will happen? Because my Schedule Apex was set to run every hour if the previous Schedule Apex can't finish inserting the data within one hour, will the next one run parallelly, or what gonna happen? 

My aim is to insert all the data automatically using Apex Scheule Job(Every hour) Adding image for better understanding

1 Answer 1

1

Yes it will run in parallel as the second job will start at its given hour irrespective of the first job is finished or now.

Scheduler is just for scheduling you class, it schedules a new instance of your batch or class everytime.

When you say inserting records, you mean new records are being inserted, so why the conflict. If you are querying some records in start and then creating some other records out of it, then to avoid this you may have to mark the records which you query with a custom field ProcessingByBatch as true in the transaction.

So that in query you only pickup records which are ProcessingByBatch as false. This means that even if the records from the First Apex Jobs are not finished your Second Apex Job will query only records which are marked as ProcessingByBatch false.

Also make sure, at the end of transaction, mark ProcessingByBatch as false so that the record does not go in hanging state ever.

4
  • Thanks for your comment. Suppose, I've 10k data. In the first Apex Sc, 5k data is inserted within one hour, then what will the second Apex Sc do? Will it reinsert all the data again? And, will the first Apex sc insert all 10k data as long as it takes?
    – Mehedi
    Commented Nov 26, 2020 at 4:16
  • 1
    When you say 10k data, what does it mean, from where are you querying the 10k data? If in the start method you are querying 10K data, and trying to create a new object from it, then yes, the first job will insert all 10K data and if the second class starts by then , even that will query the same 10K data and start acting on it, until there is a mechanism to stop this. Commented Nov 26, 2020 at 4:20
  • Thank you for your explanation. To summarize, multiple batch apex can run parallelly, right? But, how many can? It won't be a problem right?
    – Mehedi
    Commented Nov 26, 2020 at 4:37
  • 2
    There can only be 5 concurrent or parallel batches running in org. Rest will go up to a Apex queue which has a limit till 100, after which it starts throwing exception. Now its a problem or not, depends on multiple aspects and your design. So I wont be able to comment on that. But better avoid these in initial stages. Commented Nov 26, 2020 at 6:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .