What is COMMIT for?

Andwian
Member Posts: 627
I have read about COMMIT in the NAV online help, but I still do not understand.
Would someone explain it please?
Thank you.
Would someone explain it please?
Thank you.
COMMIT (Database)
Use this function to end the current write transaction.
COMMIT
Comments
When the system enters a C/AL codeunit, it automatically enables write transactions to be performed. When the system exits a C/AL code module, it automatically ends the write transaction by committing the updates made by the C/AL code.
This means that if you want the C/AL codeunit to perform a single write transaction, the system automatically handles it for you. However, if you want the C/AL codeunit to perform multiple write transactions, you must use the COMMIT function to end one write transaction before you can start the next. The COMMIT function separates write transactions in a C/AL code module.
Example
The metasyntax below contains two write transactions. As execution begins, a write transaction is automatically started. Using the COMMIT function, you tell the system that the first write transaction has ended and prepare the system for the second. Once execution has been completed, the system automatically ends the second write transaction.
BeginWriteTransactions
(C/AL Statements) // Transaction 1
COMMIT(...)
(C/AL Statements) // Transaction 2
EndWriteTransactions
Regards,
Andwian
Andwian
0
Comments
-
COMMIT need to use carefully.
Example:
Let said you have a function called CreateSalesOrder.//INSERT Sales Header SH.INIT; SH."No." := 'XXXXX'; ..... ..... ..... SH.INSERT; COMMIT; FOR i := 1 to NoOfLine DO BEGIN //INSERT Sales Line END;
Refer to the Coding above, if the Sales Line inserting got error, the Sales Header still inserted in database, because the COMMIT command executed before Sales Line that coding.
When you use COMMIT command, standard NAV's rollback feature will broken.0 -
bestianhow wrote:COMMIT need to use carefully.
Is it because how we want the system work, rollback at any point or from the beginning?bestianhow wrote:When you use COMMIT command, standard NAV's rollback feature will broken.
Once the program error, it will be rollbacked to the initial condition, or some point.
I understand now that these behaviour is just because of this COMMIT placing.
Thank you bestianhow!Regards,
Andwian0 -
COMMIT is to write your changes straight into the database. If an error occurs then this will be the last point where your system is able to do a rollback. All other changes that were made before COMMIT will remain in the database even if there's an error."Money is likewise the greatest chance and the greatest scourge of mankind."0
-
The main thing you need to learn is word TRANSACTION. Learn what does it mean. Than you will learn new two words - Rollback and Commit. Than the Commit command will be clear for you and ERROR command will be same as Rollback. ;-)
It is just about terms from database systems... :whistle:0 -
If NAV is in a process of changing data in the database this is done in a temporary (database) version. Once the whole process is complete it needs to be finalized, i.e. the temporary version should become final or ... comitted.
In general as a programmer you need not do anything for this as the system will automatically perform a commit as soon as the process is ended. This is called an implicit commit.
From a user point of view a process (write transaction) ends as soon as the focus of the system is back to the user, i.e. the user can perform his next action. For example a posting routines has ended, new value have been entered on a record and the focus is changed to another record.
Sometimes however a programmer explicitly wants to commit the changes his code made to the database long before the whole process will end. In that case you want to program a commit using the COMMIT function. This is called an explicit commit.1 -
Thank all of you for the response.lvanvugt wrote:If NAV is in a process of changing data in the database this is done in a temporary (database) version.lvanvugt wrote:Sometimes however a programmer explicitly wants to commit the changes his code made to the database long before the whole process will end. In that case you want to program a commit using the COMMIT function. This is called an explicit commit.kine wrote:The main thing you need to learn is word TRANSACTION.Regards,
Andwian0 -
No, transaction as itself. What is transaction, when it starts, what does it mean... e.g. http://en.wikipedia.org/wiki/Database_transaction0
-
Andwian wrote:When will we need the explicit COMMIT? Would you give me a scenario?... if you want the C/AL codeunit to perform multiple write transactions, you must use the COMMIT function to end one write transaction before you can start the next. The COMMIT function separates write transactions in a C/AL code module.
The scenario here is that a sales/purchase document needs to be posted and various new documents can be created likewise the Posted Sales Invoice. For these new docs a new number is to be taken from the associated number series. Taking such a number means updating the number series table(s). When updating this table the system locks this table so the posting process is the only 'user' allowed to modify this table and thereby locking out all other users/processes. They can read but not write (update). This would mean everybody else in the same company (i.e. database) can not do anything related to getting numbers from number series as long as the posting of the sales/purchase document has not ended and an implicit commit is executed. Therefor an explicit commit has beebn programmed in codeunit 80 and 90.
Just search for COMMIT in these codeunits.0 -
kine wrote:No, transaction as itself. What is transaction, when it starts, what does it mean... e.g. http://en.wikipedia.org/wiki/Database_transactionlvanvugt wrote:This would mean everybody else in the same company (i.e. database) can not do anything related to getting numbers from number series as long as the posting of the sales/purchase document has not ended and an implicit commit is executed. Therefor an explicit commit has beebn programmed in codeunit 80 and 90.lvanvugt wrote:Quote:
... if you want the C/AL codeunit to perform multiple write transactions, you must use the COMMIT function to end one write transaction before you can start the next. The COMMIT function separates write transactions in a C/AL code module.
So it is about "taking the new No. Series" and then "let the other user take a newer No. Series" - so the other users will not be locked to wait until the first user process finished, right?
Rather than "do the take No. Series" and then "do the Sales Post".
I am still confused with the phrase:if you want the C/AL codeunit to perform multiple write transactions, you must use the COMMIT function to end one write transaction before you can start the next.
I confuse with the terms "First" transaction and the "Next" transaction.Regards,
Andwian0 -
First transaction is the implicitly started transaction
Next transaction is when you want to do some next step which should be separate transaction and you need to end the previous transaction.
Transaction 1 begin
some reads and writes which are atomic
Transaction 1 end
Transaction 2 begin
some reads and writes which are atomic
Transaction 2 end
....0 -
Andwian wrote:In my understanding: please CMIIWAndwian wrote:So it is about "taking the new No. Series" and then "let the other user take a newer No. Series" - so the other users will not be locked to wait until the first user process finished, right?
For the rest: Kamil has perfectly answered your question.0 -
I have a scenario here where:
1. Register the Whse. Pick
2. Post the Whse. Shipment
are the 2 process that are separated.
Suppose that I want to combine each other, i.e. when I register the Pick, it will automatically post the Whse. Shipment.
If I just "combine" these two transaction, then if there is an error when post the Whse. Shipment (e.g. The Shipping No. Series is missing in Sales Header), then the system will rollback, just to the condition where AFTER the pick has been registered, but BEFORE the Whse. Shipment posted, i.e. I will get the Pick registered, but the Shipment not posted already.
Hence, if I want these transactions becoming one whole transaction, I should search for the COMMIT in the first (register pick) transaction, and then remark the COMMIT. So, that it will cancel to register the pick although the register is successful, when the Shipment posting is fail.
Am I understand enough the explanations?lvanvugt wrote:CMIIW? My knowledge is failing. What does that mean?
Correct Me If I am Wrong http://www.internetslang.com/CMIIW.aspRegards,
Andwian0 -
One more shotA famous example is to be found codeunit 80 (Sales-Post) and 90 (Purch.-Post).
The scenario here is that a sales/purchase document needs to be posted and various new documents can be created likewise the Posted Sales Invoice. For these new docs a new number is to be taken from the associated number series. Taking such a number means updating the number series table(s). When updating this table the system locks this table so the posting process is the only 'user' allowed to modify this table and thereby locking out all other users/processes. They can read but not write (update). This would mean everybody else in the same company (i.e. database) can not do anything related to getting numbers from number series as long as the posting of the sales/purchase document has not ended and an implicit commit is executed. Therefor an explicit commit has beebn programmed in codeunit 80 and 90.
Just search for COMMIT in these codeunits.
Is it okay to remove the COMMIT after the Sales Header get the No. Series?
Can we just view it as the one whole transaction, instead of 2 atomics transaction? Because I read from the reference, there is a phrase: "all-or-nothing" proposition.
I guess we can, but as long as we do not COMMIT, the No. Series table will be locked for the other user, until the first user in question finishing his task. Hence, it will lead to any table locking error for the other users.
Do I get this well? \:D/Regards,
Andwian0 -
Andwian wrote:One more shotlvanvugt wrote:A famous example is to be found codeunit 80 (Sales-Post) and 90 (Purch.-Post).
The scenario here is ...
Can we just view it as the one whole transaction, instead of 2 atomics transaction? Because I read from the reference, there is a phrase: "all-or-nothing" proposition.
I guess we can, but as long as we do not COMMIT, the No. Series table will be locked for the other user, until the first user in question finishing his task. Hence, it will lead to any table locking error for the other users.
Do I get this well? \:D/0 -
Of course, do not just remove the command, rather create condition, which will skip the command only for your specific case and rest will work as before!!! Adding or removing commit could totally break the system! :whistle:0
-
Locking the No Series for other users is not always a bad thing though. You can't take that and start inserting COMMIT statements all over the place because you want to prevent locks. Locks are most often necessary to keep database integrity intact.
You need to learn about transactions, and rolling back transactions.
Say a transaction contains 3 steps: step 1, step 2 and step 3. If there are no errors, and all three steps are completed successfully, the transaction is complete and NAV issues a COMMIT behind the scenes. No need to explicitly issue the command. Now if there is a COMMIT between steps 1 and 2, and step 3 causes an error, the system won't be able to completely rollback to before step 1, because of the commit. If you use COMMIT in a half-completed transaction, you are going to have MAJOR problems on your hand.
Just use this rule of thumb: until you fully understand the term "transactional integrity" and what that means for the particular job that you are working on, you are not allowed to use COMMIT. In most cases, like 99% of the time, you do NOT need to issue a COMMIT statement.
This type of thing, as well as every other question you have submitted over the past few days, are questions that your senior should address with you. You need proper guidance, and proper training. If mibuso is your only source of information, you are not getting enough help from your employer. You need to make sure that you have a senior whom you can ask these questions.0 -
Another important thing to notice as well, is that you have this code, for instance:
SalesHeader.GET(HeaderCode);
SalesLine.SETRANGE("Document No.", HeaderCode);
SalesLine.SETRANGE("sthg", "sthg");
SalesLine.FINDFIRST;
... Process what you need to ...
ReleaseSalesDocument.PerformManualReopen(SalesHeader);
SalesLine.VALIDATE(UnitPrice, MyValue);
SalesLine.MODIFY(TRUE);
You will probably get an error message on the MODIFY saying that another user has changed your record since the time you requested it (understand: in your SalesLine variable).
Try to understand why it would send a runtime error.DenSter wrote:If mibuso is your only source of information, you are not getting enough help from your employer. You need to make sure that you have a senior whom you can ask these questions.
DenSter is absolutely right: if you are in such a case that your company doesn't provide you with learning ways, consider that you'll have to do it by yourself and it's a long and painful path you should try to avoid!
This topic of implicit/explicit db transaction is one particularly hard to apprehend in NAV, if you don't have senior support. Even the NAV courses are not making things clearer, until you finally understand what's going on or somebody teaches you.0 -
Thank you all gurus for all of your valuable comments and suggestions.Denis Vitse wrote:Another important thing to notice as well, is that you have this code, for instance:
SalesHeader.GET(HeaderCode);
SalesLine.SETRANGE("Document No.", HeaderCode);
SalesLine.SETRANGE("sthg", "sthg");
SalesLine.FINDFIRST;
... Process what you need to ...
ReleaseSalesDocument.PerformManualReopen(SalesHeader);
SalesLine.VALIDATE(UnitPrice, MyValue);
SalesLine.MODIFY(TRUE);
You will probably get an error message on the MODIFY saying that another user has changed your record since the time you requested it (understand: in your SalesLine variable).
Try to understand why it would send a runtime error.Regards,
Andwian0 -
No, it is not because locks. It is because timestamps. Try to think about this:
MyRecVar1.GET(somerecord); //Timestamp=A MyRecVar2.GET(somerecord); //Timestamp=A MyRecVar2.MODIFY; //Timestamp change from A to B MyRecVar1.MODIFY; //Expecting timestamp=A but it is B => someone changed the record => Error (doesn't matter that same process changed the record)
0 -
Thanks Kamil for the explanation.
I guess I understand about the timestamp.Denis Vitse wrote:You will probably get an error message on the MODIFY saying that another user has changed your record since the time you requested it (understand: in your SalesLine variable).
Try to understand why it would send a runtime error.ReleaseSalesDocument.PerformManualReopen(SalesHeader);
so that that the Timestamp changed, while on theAndwian wrote:SalesLine.MODIFY(TRUE);
Anyway, if I want to learn about timestamp more, where can I found it? Is it about the DBMS?Regards,
Andwian0 -
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions