Saturday, 2 February 2019

Why to use Database.Stateful()?

Database Stateful is interface. It is used in batch classes to maintain the state of the class across transactions i.e. if you want to use modified class variable in execute or finish method across multiple transactions, you have to use Database.Stateful. Only instance variable can retrieve the values between transactions. Static variables resets on each transaction.

Database.Stateful affects batch performance and therefore you should use it only if it is absolutely necessary because the class will be serialized at the end of each execute method to update its internal state. This extra serialization results in longer execution time. If possible you can use alternative method to maintain state of the variable and i.e. storing value in the object and retrieving it on each transaction. 

No comments:

Post a Comment

Why to use Database.Stateful()?

Database Stateful is interface. It is used in batch classes to maintain the state of the class across transactions i.e. if you want to use ...