web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Unanswered

Allow only one batch job to insert into a table at one time in multiple batch job scenario.

(0) ShareShare
ReportReport
Posted on by 10

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.

I have the same question (0)
  • ergun sahin Profile Picture
    8,826 Moderator on at

    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.

  • pam9712 Profile Picture
    10 on at

    Thank you for the reply. I will test it.

  • nmaenpaa Profile Picture
    101,160 Moderator on at

    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.

  • pam9712 Profile Picture
    10 on at

    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. 

  • nmaenpaa Profile Picture
    101,160 Moderator on at

    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.

  • pam9712 Profile Picture
    10 on at

    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.

  • nmaenpaa Profile Picture
    101,160 Moderator on at

    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. 

  • pam9712 Profile Picture
    10 on at

    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. 

  • ergun sahin Profile Picture
    8,826 Moderator on at

    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.

  • pam9712 Profile Picture
    10 on at

    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.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 503 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 434 Super User 2025 Season 2

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 278 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans