SavePoint and Rollback will help us to maintain transaction for DML statement.
suppose you have written multiple lines of DML statements in a try block,if any error
occurs during DML Operations,the operation will be rolled back to the most recent
save point and the entire tranasction will not be aborted.
ex :
savepoint sp=Database.setsavepoint();
try
{
// create account
Account a=new Account();
a.Name='Test';
insert a;
// create contact
Contact c=new contact();
a.accountid=a.id;
insert c;
}
catch(DMLException exc)
{
Database.rollback(sp);
}
In this example, if any error occurs while inserting the Account ‘a’ or Contact ‘c’, then the entire transaction will be rolled back to SavePoint ‘sp’, as specified in the catch section by Database.
suppose you have written multiple lines of DML statements in a try block,if any error
occurs during DML Operations,the operation will be rolled back to the most recent
save point and the entire tranasction will not be aborted.
ex :
savepoint sp=Database.setsavepoint();
try
{
// create account
Account a=new Account();
a.Name='Test';
insert a;
// create contact
Contact c=new contact();
a.accountid=a.id;
insert c;
}
catch(DMLException exc)
{
Database.rollback(sp);
}
In this example, if any error occurs while inserting the Account ‘a’ or Contact ‘c’, then the entire transaction will be rolled back to SavePoint ‘sp’, as specified in the catch section by Database.
No comments:
Post a Comment