Nesting transactions

Although the syntax supports nested transactions, IBM® Product Master does not support nesting of transactions.

The following code fragment might be interpreted as an attempt to save all four entities within the same transaction, and to use a nested transaction to attempt to save the last two. The real interpretation of this code fragment is shown in the Product Master interpretation of the example column.
Table 1. Example of a nested transaction
Nested transaction example Product Master interpretation of the example
useTransaction{
	save entity to database
	save entity to database
	useTransaction{
	  save another entity to database
	  save another entity to database
	}
}
commit any existing work, 
and start a new transaction (tx#1){
	save entity within tx#1
	save entity within tx#1
	commit tx#1, and start a new on (tx#2){
	  save entity within tx#2
	  save entity within tx#2
}(commit tx#2, create a new transaction
 (tx#3)
  to replace it)
{(commit tx#3, create a new transaction
 (tx#4)
  to replace it)
Although the effect is obvious within such a small script fragment, it becomes less obvious when you consider the use of scripting libraries where the following code can be written:
Table 2. Example of attempting to nest a transaction through a library
Attempt to nest through a library Scripting library 'saveAnother2Entities'
useTransaction{
    save entity to database
    save entity to database
    call scripting library function 
'saveAnother2Entities'
  }
function saveAnother2Entities{
  //make sure we save the 2 entities in 

  //a transaction!


  useTransaction{
    save entity to database
    save entity to database
  }
}

This example is identical to the previous one, in that a call to useTransaction was made inside an existing useTransaction block, except the usage is now hidden. Both approaches cause the original transaction to be prematurely committed. When you attempt to ensure that a section of script is within a transaction and you are unsure if you are already in a transaction, use startTransaction instead.

With the addition of savepoint support to the Java™ API, it is possible to perform partial rollbacks within a transaction and achieve many of the wanted results of nested transactions. This feature is intended for use from Java code with Java API, but because Java API methods can also be accessed from scripts by using reflection, the capabilities are also available to the script developer. Consult the Java API documentation to learn how to use the savepoint methods and how to use reflection to call them from scripts.