Can multiple database operations be done in a single atomic transaction?

Joris Scharpff
Giga Contributor

I am looking for a way to perform multiple database operations as a single atomic transactions, such that either all database operations succeed or none of them. In practice this typically involves marking the start of a transaction, perform all operations and then either end the transaction if all successful or, on failure, rollback all database operations since the marked transaction start. An example in pseudo could could be:

TRANSACTION_START( );

while( record.next( ) ) {

    record.someField += 1;

    record.update( );
}

TRANSACTION_END( );

In this example we update all records we've found and set someField to its value +1. Now if any of these updates fails, e.g. due to a business rule or ACL, all of the updates should be reverted (for whatever reason). Is there any such a construction present in the GlideAPI? The features documented in the wiki do not describe such a thing, while (I think) all DB engines implement this (see e.g. Introduction to Transactions - SQLTeam.com)

Thanks in advance,

Joris

3 REPLIES 3

sergiu_panaite
ServiceNow Employee
ServiceNow Employee

Hi Joris,



We do not have this functionality on GlideAPI. We rely on the database side for all ACID operations as we are using transactional engines on the database side (as example, we use InnoDB engine on MySQL).



Regards,


Sergiu


Mike Allen
Mega Sage

You could probably accomplish this with gs.sql(), but:



Why using gs.sql is a BAD IDEA!



You have to very careful and know exactly what you are doing.


Andrey K1
Tera Contributor

5 years from starting this topic. is smth changed since then?

lack of database transactions support is really annoying and lead to overcomplexity of development.