+ Start a Discussion
TheRealistTheRealist 

Are Batches and Transaction same in batch apex?

Little confused on the terminology related to batch Apex,
while reading about batch apex in different places i see two terms BATCHES and TRANSACTIONS that are confusing me,

1) 5 transactions runs for one batch apex Job 
2) the no of records involved in the dml operation will be processed by dividing into 5 batches and by default batch size would be 200,

so i am wondering if the two statements mentioned above are same or different?

Here is an example where i am confused 

I have performed a delete operation on leads containing 250 records exactly,with the below batch apex ,and operation was successful but
while calling the batch apex ,in the execute anonymous window i have mentioned the size as 50 , then my understanding was 5 batches will run with each of them containing 50 records but , i saw total no of batches processed are 6, instead of 5,i wonder why it took 6 batches instead on 5

public class BatchLeadDelete implements database.Batchable<sobject>
{
    list<lead> llist=new list<lead>();
    public string query='select id,name,Phone from lead';
    public string flag='12345';
    
    public database.querylocator start(database.BatchableContext bc){
       return database.getQueryLocator(query); 
  }
    public void execute(database.BatchableContext bc,list<lead> le){
        for(lead l:le){
            if(l.Phone==flag)
            {
                llist.add(l);
            }
                
        }

      delete llist;             
    }
    public void finish(database.BatchableContext bc){
        
    }
 }

I called the above code as below by giving size as 50

BatchLeadDelete l=new BatchLeadDelete();
    database.executeBatch(l,50);
A Dev in NZA Dev in NZ

Hi

The Batch is the whole 250; A transaction for you contains 50 records.

A transaction is defined by the roll back, I.E. 1 transaction has it's own limits, and once succedded it cannot be rolled back; if the next transaction fails the first transaction does not roll back.

Where do you see the second statement 2), this does not seem like it has come from Batchable context, it would appear to be being used in the general form of a batch, it really should be transaction.

To me it looks like you are taking 2) out of context, hard to say.

Not sure what you are referring to as 6 batches, but more than likely they are 5 transactions of 50 and the batchable process kicking off, and I assume in the debug log. Yes they could be "Batched in 50 records per transaction, but they are 1 transaction with 1 overhead, and they are a batch of 250, they are not 1 transaction though, that would imply you can process all 250 in 1 go, but I would imagine not as you would hit limits hence you have contained them in tracasaction of 50 records.
A batch is a turn of phrase.

in debug no reference to batches

Number of SOQL queries: 50 out of 100
Number of query rows: 1700 out of 50000
Number of SOSL queries: 0 out of 20
Number of DML statements: 5 out of 150
Number of DML rows: 594 out of 10000 Maximum CPU time: 2590 out of 10000 Maximum heap size: 0 out of 6000000
Number of callouts: 0 out of 100
Number of Email Invocations: 0 out of 10
Number of future calls: 0 out of 50
Number of queueable jobs added to the queue: 0 out of 50
Number of Mobile Apex push calls: 0 out of 10



https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm?search_text=transactions

"For example, a batch Apex job that contains 1,000 records and is executed without the optional scope parameter from Database.executeBatch is considered five transactions of 200 records each."


I hope this helps