skip to main content

Programming Guides : Servlets & CGI : Make the COBOL Program Thread-safe

Make the COBOL Program Thread-safe
If a user submits a page twice in quick succession, or hits refresh before a request returns, or submits from two tabs in the same web-browser, it can cause the server to send two requests on separate threads using the same user session object. Because of this, it is possible that a COBOL program can get called on more than one thread. If the COBOL programs are not designed to handle this case, then it is necessary to prevent it using the SYNCHRONIZED and END-SYNCHRONIZED statements.
 
NOTE - One might think of adding the SYNCHRONIZED/END-SYNCHRONIZED around the CALL statement in the servlet's doPost method itself. This is not recommended because it reduces scalability. The server may reuse the same servlet object instance for multiple requests from different sessions (e.g. different users). The SYNCHRONIZED statement would cause one user to wait for another user's request to complete.
 
The solution is to add the SYNCHRONIZED/END-SYNCHRONIZED in the called COBOL program. This works because the IscobolFilter sets the COBOL thread context so that a COBOL program with a PROGRAM-ID, called with the CALL verb, will be executed on the COBOL thread associated with the user session. The servlet object instance can be reused by the server on multiple threads calling COBOL programs for different user sessions, and the SYNCHRONIZED statement in the called COBOL program will only block if two requests come in on different threads from the same user session.
A general solution is to have the servlet's doPost method call a COBOL "THRDSAFE" program that calls other COBOL programs in a SYNCHRONIZED block.
For example, add the following to the doPost method:
call "THRDSAFE" using program-name, servlet-request-obj
And code THRDSAFE.cbl as follows:
identification division.
program-id. THRDSAFE.
environment division.
configuration section.
   class HttpServletRequest  as "javax.servlet.http.HttpServletRequest"
   class ServletOutputStream as "javax.servlet.ServletOutputStream"
linkage section.
77 program-name pic x any length.
77 servlet-request-obj object reference HttpServletRequest.
77 servlet-output-obj object reference ServletOutputStream.
procedure division using program-name servlet-request-obj 
    SYNCHRONIZED
        CALL program-name using servlet-request-obj servlet-output-obj
    END-SYNCHRONIZED
    goback.

Copyright (c) 2017 Veryant
Contact us
Please share your comments on this manual or on any
Veryant product documentation with the email button at the top left