Transaction template

This section contains sample sanScript code that you can use as a template when adding transaction support to your application.

Operation

This sample code is designed to execute a set of sanScript statements within a transaction. If transaction processing isn’t available for the current database type, or if a transaction is already in progress, the sanScript statements will still be executed, just not within a transaction.

This sample code implements exception handling to deal with any deadlock exceptions that occur. If any table operations performed within the transaction result in a deadlock condition, the exception handler code will wait for at least 10 milliseconds, then retry the transaction. If the transaction isn’t successful after ten attempts, a deadlock exception is thrown to alert the user to the deadlock condition.

Using the sample code

To use this sample code, copy and paste it into your application where you want to perform the transaction. A comment near the beginning of the code indicates where to add the sanScript statements that will be performed within the transaction.

Structuring code in a transaction

The code that is executed within a transaction must have certain characteristics to operate properly. These include the following:

Refer to Guidelines for transactions for more information about designing the code to be executed within the transaction.

The following is the transaction template:

local boolean transaction_started;
local integer retry_count;
local integer MAXRETRIES = 10;
local long sleeptime;

retry_count = 0;
try
	{If a transaction can be used, start one.}
	if (Tools_GetTranLevel() = 0) and (havetransactions()) then
		transaction begin;
		transaction_started = true;
	else
		transaction_started = false;
	end if;

	{*** Statements to execute within the transaction go here ***}

	{If a transaction has been started by this code, end it.}
	if transaction_started = true then
		transaction commit;
	end if;

catch [EXCEPTION_CLASS_DB_DEADLOCK]
	{If a transaction was started, it was automatically rolled back.}

	if transaction_started = true then
		{If this code started the transaction, retry it until the
		 retry limit is exceeded.}
		if retry_count < MAXRETRIES then
			sleeptime = Timer_Sleep(10);
			increment retry_count;
			restart try;
		else
			{The retry count was exceeded. Rethrow the exception.}
			throw;
		end if;
	else
		{A deadlock exception occurred, but it wasn’t for the
		 transaction that this code started. Rethrow the exception.}
		throw;
	end if;
else
	{Some other unexpected exception occurred. If the transaction
	 this code started is still pending, roll it back.}
	if transaction_started = true then
		transaction rollback;
	end if;

	{Some appropriate error-handling code should be placed here to
	 handle the exception that occurred. This code simply displays
	 the exception.}
	error "An unknown exception occurred. Class: " + str(Exception_Class()) + " Subclass: " + str(Exception_SubClass());
end try;


Documentation Feedback