Locked Table

s.thiess.thies Member Posts: 2
edited 2005-08-09 in Navision Attain
How do I find out that another user locked the table without a runtime error.

When I write rec.Locktable(False) -> runtime error.

When I write rec.Locktable(true) -> the client wait and wait and wait until the othe finishes, but i want something else. I want to react when the table is locked.

Comments

  • krikikriki Member, Moderator Posts: 9,112
    Some trick but only for Navision DB:

    Create a codeunit ("Try To Lock A Table") with as parameter the object table.

    In the OnRun of the codeunit:
    CASE rec.ID OF
    3: rec3.LOCKTABLE(FALSE);
    36: rec36.LOCKTABLE(FALSE);
    37: rec37.LOCKTABLE(FALSE);
    END;
    // you can also use RecordReference here.
    // Like you said : if it is locked, LOCKTABLE(FALSE) will generate an error

    to test if the table is locked, do this:
    recObject.ID := DATABASE::"Table To Be Locked";
    IF CODEUNIT.RUN(CODEUNIT::"Try To Lock A Table",recObject) THEN BEGIN
    // Table is not locked

    ENDELSE BEGIN
    // Table is locked

    END;

    // the "IF CODEUNIT.RUN(" will intercept errors generated in the codeunit and return FALSE in that case.
    // IMPORTANT : any writes to the DB in the codeunit BEFORE the error, will NOT be undone by the error!!!!
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • fbfb Member Posts: 246
    Alain,

    I believe there is a problem with your suggestion.

    Whenever you code
    IF CODEUNIT.RUN(...)...
    
    then the following conditions apply
    • A write trx may not be open before the statement, and
    • An implicit COMMIT occurs before the next statement.
    Doesn't the implicit COMMIT release all locks, so you cannot say
    END ELSE BEGIN
    // table is locked...
    
    ?
  • krikikriki Member, Moderator Posts: 9,112
    A write trx may not be open before the statement, and
    What do you mean with this? Navision will not complain if a trx is open and if the trx contains a locktable on the table you want to test, it will generate an error that is intercepted by the "IF CODEUNIT.RUN...".

    An implicit COMMIT occurs before the next statement.
    There is only an implicit COMMIT when all action stops and the screen is released so the user can work again, so no problem for this one.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • fbfb Member Posts: 246
    Here's where I've gotten my ideas from:

    A) A write trx may not be open before the statement: I cite two sources:

    1) C/SIDE Reference Guide (on-line help), topic titled 'Codeunit.RUN' contains the following snip:
    If you use the return value in an IF statement which is inside a write transaction, a run-time error occurs unless you commit your data updates before you call Codeunit.RUN.

    2) Fin.ext contains the following error string:
    CodeUnit.Run() is allowed in write transactions only if the return value is not used. For example, 'OK := CodeUnit.Run()' is not allowed.

    B) An implicit COMMIT occurs before the next statement: Again two sources:

    1) C/SIDE Reference Guide, topic titled RUN (Codeunit) contains the following:
    The transaction that the codeunit contains is always committed due to the boolean return value.

    2) Think about it: If the construct traps errors, that implies that any trx started in the codeunit is rolled-back. Suppose now that I call a codeunit more than once using the IF construct -- the first time everything goes ok, but the second time an error occurs. If no implicit commit occurs after the first call, then what does the error on the second call roll-back -- the work of the second call only, or both the second call and the first call? If the work of the first call is not implicitly committed, then how can I call the codeunit again (using the IF construct) without committing? Rpt 496 - Batch Post Purchase Orders does just this (forget the actual commit near the end of this codeunit -- consider instead the work done after it in UpdateAnalysisViews, which since v3.70 no longer contain COMMITs).
  • krikikriki Member, Moderator Posts: 9,112
    I checked it out these particularities and you are right, they don't work.

    Years ago, I have something similar in a NAS. To fix it, I wrote a message that arrived in the event-log of Windows and let the NAS crash on the error. Windows restarted the NAS automatically and the NAS retried and in generally after some time also succeeded to do the operation.

    I am afraid that there is no way to know if a table is locked. Or you let it crash or you wait.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • jevgjemjevgjem Member Posts: 17
    kriki wrote:
    Years ago, I have something similar in a NAS. To fix it, I wrote a message that arrived in the event-log of Windows and let the NAS crash on the error. Windows restarted the NAS automatically and the NAS retried and in generally after some time also succeeded to do the operation.

    Hello.

    I have a problem with NAS - it crashed with error, and I need to restart it manually every time. CAn you tell me, please, how you made Windows to restart automatically NAS after error?

    About "Test locktable" - I've tried many solutions - and have no success in any. ](*,)
  • krikikriki Member, Moderator Posts: 9,112
    In the "Services" of Windows, open the properties of the service.
    In tab "Recovery" you just have to put in the failure fields : "Restart The Service".
    PS I have W2K professional, so if you have another version, it may be a little different.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • jevgjemjevgjem Member Posts: 17
    kriki wrote:
    In the "Services" of Windows, open the properties of the service.
    In tab "Recovery" you just have to put in the failure fields : "Restart The Service".
    PS I have W2K professional, so if you have another version, it may be a little different.

    Well, I have tested this solution - for me it does not work, because , as I understand, when there are some error in Application server - it comes to Events viewer not with type "error", but with "warning" type. So, in this case W2K does not restart service. How you succeed to made a crash of App service?
  • krikikriki Member, Moderator Posts: 9,112
    The codeunit I launched in the NAS was NOT singleinstance. To keep running the codeunit, I had a loop in it with a SLEEP in it to not run it all the time. At a certain moment the loop noticed some problem and in this moment I just put an ERROR-statement in it. This generated the error and got out of the NAS. (I also didn't have a "IF CODEUNIT.RUN(...) THEN .." to run my codeunit, because this would intercept the ERROR I generated.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


Sign In or Register to comment.