Expand my Community achievements bar.

SOLVED

How to rollback after an insert operation was made in Javascript activity?

Avatar

Level 2

Hi all.

I would like to ask how to rollback a transaction i.e inserted recipient made if there's an error thrown inside Javascript activity?

I found rollback() function here: https://final-docs.campaign.adobe.com/doc/AC/en/jsapi/m-DBEngine-rollback.html however, I'm not sure how to use it or if I can use it.

If there'll be a sample code, it would greatly help. Thank you so much.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi Mary,

by my opinion xtk.session.Write() is final statement (after execution an automatically COMMIT is done).

It is best option to open one ticket towards Adobe Support to clarify usage of rollback().

You might try to create a procedure on DB level (basic Oracle example below) and then adapt your web service to invoke procedure and then everything will be done by your database.

CREATE PROCEDURE foo (x NUMBER) IS
BEGIN
  SAVEPOINT update_bar
;

 
-- Do some inserts here.
 
INSERT INTO bar VALUES (x);
 
-- Sometimes there might be an error.
 
IF x = 3 THEN
  RAISE_APPLICATION_ERROR
(-20000, 'Wooops...');
 
END IF;
EXCEPTION
 
WHEN OTHERS THEN
  
-- Rollback everything which was made after `SAVEPOINT update_bar`
  
ROLLBACK TO update_bar;
  RAISE
;
END foo;

Regards,

Milan

View solution in original post

3 Replies

Avatar

Community Advisor

Hi Mary,

you may use dbEngine class to work with sql queries from JS.

var dbEngine = instance.engine

dbEngine.exec("UPDATE your_table SET column = 1 WHERE 1=1")

Therefore, it is possible to use something like dbEngine.rollback() but not after the code above  because commit is done automatically. You have to write procedure in order to use commit or rollback after transaction.

Regards,

Milan

Avatar

Level 2

Hi Milan.

Thank you for your response.

I'm using xtk.session.Write() to insert/update a schema in a jssp.

(Still new in Campaign and so far I have been using this function when doing insert/update/delete.)

A bit of background: This jssp file is being called by AEM as a webservice.

It has a series of processes which involves search of schema and insert/update record.

In case there's an error thrown, I would like to rollback all the insert/update made before the error is encountered.

Is it possible to do a rollback after that?

Avatar

Correct answer by
Community Advisor

Hi Mary,

by my opinion xtk.session.Write() is final statement (after execution an automatically COMMIT is done).

It is best option to open one ticket towards Adobe Support to clarify usage of rollback().

You might try to create a procedure on DB level (basic Oracle example below) and then adapt your web service to invoke procedure and then everything will be done by your database.

CREATE PROCEDURE foo (x NUMBER) IS
BEGIN
  SAVEPOINT update_bar
;

 
-- Do some inserts here.
 
INSERT INTO bar VALUES (x);
 
-- Sometimes there might be an error.
 
IF x = 3 THEN
  RAISE_APPLICATION_ERROR
(-20000, 'Wooops...');
 
END IF;
EXCEPTION
 
WHEN OTHERS THEN
  
-- Rollback everything which was made after `SAVEPOINT update_bar`
  
ROLLBACK TO update_bar;
  RAISE
;
END foo;

Regards,

Milan