Tuesday, September 2, 2014

Dexterity - modified Transaction template for when you require a transaction

This is from the Microsoft Dexterity Help 12.0b2 in the Transaction template page. I have modified it so that it only proceeds with the logic if it can start a transaction. 

A good, clean design has error handling separated from the business logic, so it calls DoLogic() which does the actual business logic. Errors bubble up to this error handler. This keeps the transaction / error handling away from the logic and really clarifies the business logic as well as the error handling.

 

{The Transaction Template}
local integer retry_count;
local integer MAXRETRIES = 3;
local long sleeptime;

retry_count = 0;
try
    if CanStartTransaction() then
        transaction begin;
  if DoLogic() then
                 transaction commit;
  else
   transaction rollback;
  end if;
 else
  warning "DoLogic() must be executed within a transaction, but one cannot be started."
    end if;

catch [EXCEPTION_CLASS_DB_DEADLOCK]
 
     if retry_count < MAXRETRIES then
         sleeptime = Timer_Sleep(10);
         increment retry_count;
         restart try;
     else
         throw;
     end if;
    
else
    transaction rollback;
    error "An exception occurred. Class: " + str(Exception_Class()) + " Subclass: " + str(Exception_SubClass()) + " Message:" + Exception_Message();
end try;

No comments:

Post a Comment

There was an error in this gadget