cancel
Showing results for 
Search instead for 
Did you mean: 

handling rollback in transaction

Former Member
0 Kudos

Hi everybody,

Does anyone knows if the Add function (like in JournalEntry.Add()) ends a transaction when it fails?

Thanks

Ronen

Accepted Solutions (1)

Accepted Solutions (1)

rasmuswulff_jensen
Active Contributor
0 Kudos

Hi Ronen.

Yes. A business object manipulation (Add/update...) end a global transaction if it fails (By definition)...

I guess you have some batch-run that you need to run a global transaction around it, but you want to loop through everything in order to give a more detailed error-log (Common problem) - If that the case try this.

1. Begin transaction

2. create a boolean to determine if any errors occured

3. Begin loop

4. Do business object manipulations

5. If business object manipulations fails set the boolean to true (error occured) and begin a new global transaction (This is used cancel futher new business transactions that goes well)

6. After loop, check if boolean have detected any errors during the run. If error, du a global endtransaction and rollback (And show errors), else do a endtransaction and commit...

Former Member
0 Kudos

Hi Rasmus,

I try to add add journal entries.

How can i use the rollback option if the error that accured ended the transaction?

it gives me the error "there is no transaction in progress"

Ronen

rasmuswulff_jensen
Active Contributor
0 Kudos

You can use the Company objects InTransaction property


if(oCompany.InTransaction) {
  oCompany.EndTransaction(<rollback or commit>)
}

Former Member
0 Kudos

Hi Rasmus,

In the transaction i loop through a datatable rows and add the entries.

I want to rollback if one of the entries failed.

You told me that the Add function ends the transaction when it fails.

Is it possible to do what i want?

Ronen

rasmuswulff_jensen
Active Contributor
0 Kudos

Yes... look in step 5 in my previous reply... Here I start a new global transaction after the business object Add method failed (in order to complete the loop to get all errors)... It's a bit complicated to picture in your mind that after a failure you need to start a new transaction, but it works (I normally draw an illustration of the loop to get an overview of it)... I've used it many times, and it works...

Former Member
0 Kudos

But how can i rollback if its a new transaction?

It wont rollback the entries that were added ok

rasmuswulff_jensen
Active Contributor
0 Kudos

Yes it will...

0 - bool errors_on_the_way = false

1 - Start transaction

2 - Add #1 (ok)

3 - Add #2 (ok)

4 - Add #3 (fail (All are rolled back since last start transaction))

5 - Start new transaction as part of #3 fail and remember that an error occured (errors_on_the_way = true)

6 - Add #4 (ok)

7 - Add #5 (ok)

<no more>

if(errors_on_the_way) {

//Rollback transaction (Roll back #4+5 (#1-3 is already rolled back))

}

else {

//Commit transaction (Only in none fails)

}

In this case this will result in no add to the company since #3 failed (And all 5 was tested if they could be added)

Message was edited by: Rasmus Jensen

Former Member
0 Kudos

you mean that the error automatically rollback everything since the last start transaction?

rasmuswulff_jensen
Active Contributor
0 Kudos

Yes...

Take a look at this image

http://www.rwj.dk/transaction.jpg

Answers (0)