Notifications
Announcements
No record found.
Hi,
I have created a batch job class based on RunBaseBatch. When I run this class, it populates new report data into a regular table. I can't use other table types and it has to be a regular table. Also, this table will only be used by this class only. I have the logic in the class where it deletes everything from the table and recreates it on each run. Obviously, It shows inaccurate data when there are multiple reoccurring batch jobs runs at the same time. How can I solve this issue that only one batch can go into this table and others will have to wait or one can go in and others jobs will fail.
Thank you.
There are probably many solutions, but I'll try to describe the first that comes to my mind.
Add a variable to Class and fill it with a uniq value when batch runs. Add this value to the table and only take action on the relevant records.
In this way, multiple batches can run simultaneously. Of course, you must take the new field into account for all select clause.
Thank you for the reply. I will test it.
You can also add a column for Session id in that table so that each session will have their own records. I think this is the approach that Microsoft uses in some report data tables that are not temporary tables.
This does not work as I only want to run one batch at a time and fail other batches if they are run concurrently. User will receive the message in the batch log that the batch failed due to another batch was running. Also, I want to keep the data in a table from one batch.
Why doesn't it work? If a batch notices that this table has data with other session if, you can make it fail. Or am I missing something?
But someone / something must eventually clear data from the table or all future batches would fail. But I guess you already must have thought of this.
I personally think that your approach is quite different than the overall logic of the system, but it's definetely doable if your business requirement truly needs it.
I want to keep the data in the table until the next batch job comes and overwrites it. Because the batch job can be set using different parameters. I don't want to keep the different versions of data in that table, only the latest run will be kept. Everything works fine but it only fails if the user setup multiple batch jobs which run at the same date&time. In this scenario, I only want one of the batch to execute and the rest to show an error.
You can either try to use BatchJob and Batch tables to figure out if the batch is already running, or let your batch write it's status (executing /done) in some other table. This way other batches know whether to fail or not. Should be pretty straight forward. And the moment when the batch is done, another batch is free to delete the data in your table and re-populate it.
That is what I am trying to do, I am using the batch table with the following statement:
select from batch where batch.ClassNumber == classNum(myBatchClass) && batch.Status == BatchStatus::Executing; NewBatchRunStatus.BatchJobId = batch.batchjobid; NewBatchRunStatus.Caption = batch.Caption; NewBatchRunStatus.Status = BatchStatus::Executing; NewBatchRunStatus.insert(); try { if (NewBatchRunStatus.Status != BatchStatus::Executing) { error("Another batch running the same batch class"); } else { //run logic and insert data into my table While select * from NewBatchRunStatus where NewBatchRunStatus.Status = BatchStatus::Executing { NewBatchRunStatus.Status = BatchStatus::Ended NewBatchRunStatus.update(); } } } catch (Exception::Error) { }
I am using another table to write the batch info such as batchjobid, status, etc. But when another batch job runs at the same time and executes the above code, it fails.
It can be managed with batch information(from standart batch tables), but I think it is necessary to think more simply and avoid surprises.
If the table holds temporary data, delete it at the end of the batch. If there is a record in the table, dont let run the second batch.
If the records are needed after the batch is over. Create a field, fill the field at the end of the batch. If there is a record and the field is not full, dont let run the second batch.
The table needs to hold data until the next batch run. But Ergün, your logic will not work because if one or more batches run at the same time and line by line, the second batch will overlap and the result in the table will be inaccurate.
Under review
Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.
As AI tools become more common, we’re introducing a Responsible AI Use…
We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…
These are the community rock stars!
Stay up to date on forum activity by subscribing.
Martin Dráb 503 Most Valuable Professional
André Arnaud de Cal... 434 Super User 2025 Season 2
BillurSamdancioglu 278 Most Valuable Professional