cancel
Showing results for 
Search instead for 
Did you mean: 

StartTransaction and EndTransaction

Former Member
0 Kudos

I need to import a XML file, with Journal Entries, several times in a month.

The file has about 100 Journal Entries and, for example, if the 80th, gives an error, after that, the process stops and has to "rollback" the others 79.

First of All, when importing I begin the transaction:

oCompany.StartTransaction()

....

'After add the lines

oJournalEntries.Lines.Add

...

'After fill the header, add the entry

oJournalEntries.Add

...

The problem is that after we .Add(), it commits the transaction and we cannot rollback.

Could someone help how can I do that? Or How can we use the oCompany.StartTransaction()

Ribeiro Santos

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

Hello,

below is an code sample that shows how you can handle global transactions with DI API:

'//starts transaction - each failoure will cause rollback

'// 'InTransaction' property will turn to TRUE

oCompany.StartTransaction

For i = 0 To Count

'Add Journal Entry

'=================

TransID = CreateJournalEntry(i)

'//In case of failure of a single transaction (e.g creation of the Journal Entry)

'//the transaction will be rolled back automatically:

'//1) all the former changes in the database will be canceled (delete all the journal entries 0...i-1)

'//2) 'InTransaction' property will turn to FALSE

If Not (oCompany.InTransaction) Then Exit For

'//although the transaction was terminated the code will continue from the same location

'//(journal entries i+1.....count will be created)

'//it is the developer responsability to skip to the end of the transaction (in this example, 'Exit For')

Next i

If oCompany.InTransaction Then oCompany.EndTransaction wf_Commit

'//'EndTransaction' method cannot be executed if the transaction is not active

'//meaning if the transaction was rolled back automatically there is not need to end the transaction

'//it can be check by checking the value of 'InTransaction' property

as it can be understood from the sample, in case of an error within the transaction, the transaction will be terminated AUTOMATICALLY, the devloper is not expected to call 'EndTransaction' method with wf_RollBack as parameter

the parameter wf_RollBack enable the user to rollback the whole transaction in case there was no error in a single transation, but from some other reason the transaction has to be cancelled anyway

Regards, Avi.

Former Member
0 Kudos

Hi,

I found the problem:

I just Call oCompany.StartTransaction once (at the begin of the process), after, when I get the first error, the transaction ends, and like I continues to Add Journal Entries, B1 "auto commit" these transactions.

For each JournalEntries I have to StartTransaction, Add the JE, and then End the transaction.

Thanks,

Ribeiro Santos

Former Member
0 Kudos

Hi,

If that's what you want, then why use transactions at all?

Transactions are intended to be able to jointly commit a set of actions, provided everything along the route went well. If you start and end and start again etc. you cannot commit the whole set at once! You will have commited all up to the occurring error and if I read you correctly this is what you want to prevent.

I think, you should monitor for errors and as long as no error occurs (or as Frank says as long as Intransaction=true), just continue with the next JE. Completely at the end, perform a commit after all criteria you want have been met.

Hth,

Jacques

@Frank M: early version did not over this; I was most unsatisfied with the contents of the course

Former Member
0 Kudos

What I want is to post all transactions if, and just if, it does not find any error. If I found an error, the process don´t import any transaction.

I think the best way is pass twice the file XML:

First, to see if there are some error in some JournalEntry

Second, if no errors found, begin to post all

JournalEntries.

Any better way to do that?

Ribeiro Santos

Former Member
0 Kudos

Hi,

That's just what I'm saying:

1) start transaction

2) process all, catch errors (by checking return values)

3) at the end if no errors occurred, commit transaction

if an error occurs, thransaction will rollback and nothing will be in database.

So if everything goes well: 1 pass only

Hth,

Jacques

Former Member
0 Kudos

Sorry, I forgot to say that, I have to "test" all the Journal Entries, to inform the user what are the errors.

Them the user should correct them and then import again.

That's why I have to pass twice. But, yes, if no errors found them is just one pass.

Ribeiro Santos

Former Member
0 Kudos

Hi,

Can you show us the line of code you're using when you try to rollback?

I've been using the transaction feature a lot and I heven't had any problems yet (after I got it working). But I'm not using it on journal entries.

Except that SBO will crash after there are several unfinished transactions (stopping debugger). Also nested transactiosn do not seem possible.

Regards,

Jacques

former_member185703
Active Contributor
0 Kudos

Hi,

Please note that you have to check every return value e.g. of transactions like "Add" - regardless whether or not you use global transactions / the StartTransaction-EndTransaction mechanism or not!

The global transaction mode will be terminated whenever DI API encounters any error (exception or error indicated via return codes).

All transactions performed afterwards are then single transactions - without further notice. Company object's "InTransaction" property indicates whether or not the global transaction is still open.

If you try to call EndTransaction in such a situation, an exception will be thrown, telling you that the connection would not be in a (global) transaction.

Please show us more code, if this does not help you.

HTH,

Frank

PS: SDK classroom training also covers such questions.