SBX - Search With Button

SBX - Forum Post Title

While Try catch

Microsoft Dynamics AX Forum

luis torres asked a question on 8 Nov 2018 3:31 PM

Question Status

Verified

hi everyone. ithis may be a strange request but i'm really curious about this issue(if it can be called like that) that I have.

as a context, I'm reading an input file to make some insertions in the database.

while coding, seemed to me a good idea to read the file and if a line is wrong then skip it giving a warning about what's wrong in which line and read the next one, of it's ok then make the insert. after came the idea "wait, what if there is 500 lines and just one it's ok... (been extreme) that will be confusing for the user about what's ok and what's not or which lines where inserted cause that will show up a really big Infolog... that's gonna be more troublesome than helpful" so it's better to check the file and if something it's wrong then don't insert anything.

but I'm not sure what approach should I use... I mean there is more than one way of doing it, is there any "best practice" of how to make this?  a way to see what's more effective? or it doesn't really matter since the size file ain't that big to cause any performance problem at all no matter the way i choose to do the charge.

cause right now i have two ways of doing this in my mind:

the Current way(which i already said it may go wrong in certain circumstances):

read the records from the file, validate the data and if it's ok then insert, if not then go to the next record, the lines that are wrong goes to the info log to be fixed and loaded in another file.

another way 1:

read the records from the File, validate all the lines of the file, if ok then read the file again and make the insert to the database, if there is an error in the data put it in the info log to fix it and reload the file

another way 2:

read the records from the file, validate the data and if it's ok then do the insert, if not, then rollback previous insertions, don't allow inserts for the next lines and continue just checking the file for error so they can be fixed, errors will be thrown in the info log.

another way 3(came to me while writing this, a variation of my current process):

My current Process, just instead of throwing various errors in the info log put them in an output file, which will contain exactly the input line(s) of error, plus one last column with the warning that error that I was displaying in the info log in the first time.

as part of this issue, if iIuse the next syntax:

try

ttsbegin;

while

{

//read current line

//insert info into database with an  ax class

}

ttscommit;

catch

{

throw error("bad information");

}

if there is an error let's say on the last line of 100, the 99  inserted records are rollbacked? I'm aware that the "throw error" does a rollback, but I'm not sure if this rollback works correctly if the insertions are been made inside a while loop, I ca't Find an example like this in the documentation.

another thing I'd like to ask (a bit related to this) is

I'm Using classes from ax to make the insertion to the database, (axInventJournalTrans in this case) what's the purpose of this kind of classes in ax? what's the difference between this and a regular insert into SQL clause?

Reply
luis torres responded on 8 Nov 2018 3:31 PM

sorry for the long post btw

Reply
ievgen Miroshnikov responded on 8 Nov 2018 8:56 PM
Verified Answer

Hi luis torres,

First of all, let answer on transaction question. If you have any code inside of the transaction that throws Exception::Error, it will rollback the transaction and it does not matter if has a loop or not.

Now, generally it's a bad idea to read file line by line and insert because file IO could be slow and you creating big transaction for a long period of time. To avoid this usually you want to read it lets say to a container or a list, validate data and then insert into a table. You can validate and insert only valid records or stop process after first invalid record, but it's more about your business requirements than best practice.

Ax classes are used by AIF and parm* and set* method could have some logic, to init some fields base on other fields, you can use them or assign values directly to table fields and invoke all required methods on your own.

Reply
luis torres responded on 9 Nov 2018 9:45 AM

hi ievgen thanks for your reply.

sorry for not been specifing in this part. this is how i'm working with the file:

While(InFile.status() == IO_Status::Ok)

{

   copy the data into a container

   validate the data from the container

   if ok insert record in table

   if not show what's wrong in what line

}

is this what you mean by putting into a container or is better this way? (this is how i'm picturing it at least)

While(InFile.status() == IO_Status::Ok)

{

   copy the data into a container (with an extra field to mark if info it's ok or not)

   Validate the data from the container

}

after you copy all file data into the container

while(read the container "line by line")

{

   if data ok then insert

   if not show what's wrong in what line;

}

Reply
ievgen Miroshnikov responded on 9 Nov 2018 12:23 PM

Yes, you got the idea.

Reply
luis torres responded on 9 Nov 2018 12:27 PM

second option is better then?

Reply
ievgen Miroshnikov responded on 9 Nov 2018 12:31 PM

Yes. But that's only my opinion. 

Reply
luis torres responded on 9 Nov 2018 1:07 PM

thanks for your time :) i'll give it a try

Reply
ievgen Miroshnikov responded on 8 Nov 2018 8:56 PM
Verified Answer

Hi luis torres,

First of all, let answer on transaction question. If you have any code inside of the transaction that throws Exception::Error, it will rollback the transaction and it does not matter if has a loop or not.

Now, generally it's a bad idea to read file line by line and insert because file IO could be slow and you creating big transaction for a long period of time. To avoid this usually you want to read it lets say to a container or a list, validate data and then insert into a table. You can validate and insert only valid records or stop process after first invalid record, but it's more about your business requirements than best practice.

Ax classes are used by AIF and parm* and set* method could have some logic, to init some fields base on other fields, you can use them or assign values directly to table fields and invoke all required methods on your own.

Reply

SBX - Two Col Forum

SBX - Migrated JS