B4J Question Rollback method has replaced EndTransaction for SQL Class

j_o_h_n

Active Member
Licensed User
tl,dr 1: In the SQL class, the Rollback method has replaced EndTransaction.
2: BeginTransaction does not always begin a new Transaction.

When I was looking at SQL examples here I came across some that made use of the EndTransaction method
However this method no longer exists for the SQL class. I could see that the other two transaction related methods
BeginTransaction and TransactionSuccessful were still there but there were no other methods with Transaction in their
name (I did not notice Rollback for some reason). I think I asked a question but no one picked up on it so I thought
OK, that must be how it is. But of course it isn't the case and here is how it bit me when unparameterised data caused
an exception:
B4X:
Sub Timer1_tick()
   
    Try
       SQL1.BeginTransaction
       If some_value_in_database = false then
            run_database_update_query_to_make_it_true
            Make_some_other_database_updates    '<--------exception occured here
       end if
  
        SQL1.TransactionSuccessful
    Catch
        'I should have had SQL1.Rollback here to handle this correctly
        Log(LastException.Message)
    End Try
End Sub

On the first tick event an exception is thrown but on the second none is. The result is that the query on line 5 is committed to the database but the query on line 6 isn't.
An interesting second point is that it's still the same transaction in both calls even though SQL1.BeginTransaction is run each time.
I hope this might help someone sometime who's wondering where EndTransaction has gone.
 
Top