+ Start a Discussion
pinal2pinal2 

Apex Batch

Hi Everyone,

 

I am trying to understand how Apex Batch works. Does Apex Batch do Job partioning? Below snippet is from the following link: http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content%2Fapex_batch_interface.htm|SkinName=webhelp

 

"Each execution of a batch Apex job is considered a discrete transaction. 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. The Apex governor limits are reset for each transaction. If the first transaction succeeds but the second fails, the database updates made in the first transaction are not rolled back."

 

Based on above snippet, does Apex Batch executes the five transactions at a same time, like parellel processing? Or does it just executes the chunk of 200 records at a time sequentially? I am assuming it runs all five parllely. Please shed some lights.

 

Also, the start method of Database.Batchable Interface returns QueryLocator, does it return all the data in one call? Just trying to understand what QueryLocator does.

 

Thanks so much in advance! I'll look forward to your postings :-)

 

Regards!

Best Answer chosen by Admin (Salesforce Developers) 
Anand@SAASAnand@SAAS

Query Locator is similar to a Database cursor or a JDBC Resultset. The Query used to create the query locator can return upto 50 Million rows. But for each invovation of "Execute()" a maximum of only 200 records will be passed to it. 

 

Apex batch processes records sequential i.e. if you have 1000 records returned in your Query Locator, there will be 5 batches that run one after another (assuming you process 200 records at a time).

All Answers

Imran MohammedImran Mohammed

Hi Pinal,

 

I think that the transactions run sequentially.

And the query can return the QueryLocator which can have upto 50 million records.

 

Regards,

Imran

Anand@SAASAnand@SAAS

Query Locator is similar to a Database cursor or a JDBC Resultset. The Query used to create the query locator can return upto 50 Million rows. But for each invovation of "Execute()" a maximum of only 200 records will be passed to it. 

 

Apex batch processes records sequential i.e. if you have 1000 records returned in your Query Locator, there will be 5 batches that run one after another (assuming you process 200 records at a time).

This was selected as the best answer
pinal2pinal2

Thanks guys for explaining it very well!

 

Regards!

RamyaKrishnaRamyaKrishna

to perform batch apex we use 3 methods.

1. start()

It reruns all the records that are used in batch apex.

Return type is Database.QueryLocator. Its nothing but list of sobject

It returns all the records to execute()

2. Execute()

It divides all the records in batches.

Maximum batch size is 200 records.

3. finish()

It can be used to know the status of the batch apex.

Generally this method is used to send the confirmation mails

Note: Batch apex class should be implemened by Database.batchable interface and this should be global

   All methods used in this class is global.

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_batch_interface.htm