Achieve exclusive control of datasets in a multithreaded Java environment (WAS for z / OS environment) on IBM mainframes

Overview

Using JZOS [reference link 1] , You can develop Java programs that input and output data sets in z / OS environment (IBM mainframe environment) [Reference link 6 ]. For Java applications that run in a single-threaded environment, there is no problem if you leave the exclusive control of the dataset to the OS. The DISP parameters (SHR / OLD) specified when allocating the dataset are exclusively controlled as shown in the table below, so there is no need to implement special control logic as an application. Exclusive control in this case is exclusive control between processes, and is the same as exclusive control when executing a batch program written in COBOL or PL / I in JCL (** <Data set in COBOL batch below). Exclusive control> **).

New request\Processing in progress Reading(SHR) Writing(OLD)
reading(SHR) × (Note 1)
writing(OLD) × (Note 1) × (Note 1)

** Note 1 ** In the case of JCL, if an access request conflicts, a new request will be in a waiting state, and it will be accessible when the data set is released after the processing being executed is completed, but the dynamic allocation by BPXWDYN If so, an immediate error will occur. In addition, it is customary to specify DISP = OLD at the time of writing and DISP = SHR at the time of reading when exclusive control is performed by JCL, and even if write processing is performed with DISP = SHR, an error will occur due to itself. is not.

** ** As shown in the figure below, when COBOL batch # 1 of job # 1 attempts to access the data set being written with DISP = OLD, COBOL batch # 2 of job # 2 attempts to access it with DISP = OLD. COBOL batch # 2 waits for the dataset to be freed.

image.png

However, in a Java program in a multithreaded environment, it is not possible to leave all exclusive control to the OS. The following describes the issues related to access conflicts when working with datasets in a multithreaded Java environment and the solutions using the JZOS API with samples.

Challenges in a multithreaded Java environment

When accessing a dataset from a Java program in a single-threaded environment, no special action is required regarding conflicts, but a server-side Java application that runs on WebSphere Application Server for z / OS (hereinafter referred to as WAS for z / OS). Care must be taken when accessing the dataset from. This is because the WAS for z / OS environment is a multithreaded environment (Java EE environment). The above-mentioned exclusive control by the OS is a story when accessing the same data set between multiple processes, and when accessing the same data set from multiple threads in the same process, it is the responsibility of the application to implement the exclusive control logic. It is necessary to prevent data corruption (see ** <Issues of data set exclusive control in Java batch on WAS for z / OS> ** below).

** <Issues of data set exclusive control in Java batch on WAS for z / OS> ** Exclusive control of Java batch # 1 and Java batch # 2 in the figure below works effectively, but multiple threads in the same address space (corresponding to Unix / Window process) such as Java batch # 1 and Java batch # 3. It is the responsibility of the application to implement exclusive control between them.

image.png

Implement exclusive control logic in com.ibm.jzos.Enqueue class

Then, how can you implement exclusive control? This is also the method provided by the JZOS API (com.ibm.jzos.Enqueue class] [Reference link 2. /knowledgecenter/SSYKE2_8.0.0/com.ibm.java.zsecurity.api.80.doc/com.ibm.jzos/com/ibm/jzos/Enqueue.html)].

The sample program ʻEnqTest.java` is shown below. In order to convey the main points of implementation, the sample below takes the form of a simple stand-alone Java application, but the logic described in the main method can be used almost as it is in a Java EE application (** Note 2 **).

EnqTest.java


package sample;
import com.ibm.jzos.ZFile;
import com.ibm.jzos.Enqueue;
import javax.xml.bind.DatatypeConverter;

public class EnqTest {
	public static void main(String args[]) throws Exception {
		String qname = "TEST_Q01";
		String dsName = "USER001.TEST.DATA1";
		Enqueue enq = new Enqueue(qname, dsName);
		enq.setScope(Enqueue.ISGENQ_SCOPE_SYSTEM);
		enq.setControl(Enqueue.ISGENQ_CONTROL_SHARED);      // <-In the case of reading process
		//enq.setControl(Enqueue.ISGENQ_CONTROL_EXCLUSIVE); // <-For write processing
		enq.setContentionActWait();
		
		try{
			enq.obtain();
			byte[] token = enq.getEnqToken();
			System.out.println("token:"+DatatypeConverter.printHexBinary(token));
			
			//Describe the data set input / output process here
			
		} catch(Throwable t) {
			t.printStackTrace();
		} finally {
			if (enq.getEnqToken() != null) {
				enq.release();
			}

		}
	}

}

** This is the point! ** The points of the sample program are explained below. Since the above sample focuses on exclusive control, I / O processing of the dataset is omitted. For those who want to know the specific contents of the data set input / output process, "Reference link 6. Try to access the data set from Java using JZOS" / 8f75ce6a2f52c6c61ff5) I would like you to check it.

--Exclusive control is implemented using the com.ibm.jzos.Enqueue class provided by JZOS ** (Note 3) . --Create an Enqueue object by specifying the queue name and resource name in the constructor. --Specify a queue name of up to 8 characters. --Specify a resource name of 1 to 255 bytes. --Set one of the following scopes with the Enqueue # setControl method. The exclusive control target resource is identified by the specified scope and the above ** queue name + resource name , and enqueued / dequeued ** (Note 2) . - Enqueue.ISGENQ_SCOPE_STEP ** in the address space - Enqueue.ISGENQ_SCOPE_SYSTEM ** Within a single system (default) - Enqueue.ISGENQ_SCOPE_SYSTEMS ** Overall Sysplex -** Enqueue.ISGENQ_SCOPE_SYSSPLEX ** Whole sysplex (same as SYSTEMS) --Specify one of the following exclusive control modes with the Enqueue # setControl method. -** Enqueue.ISGENQ_CONTROL_SHARED ** Shared mode (specified for read processing) -** Enqueue.ISGENQ_CONTROL_EXCLUSIVE ** Exclusive mode (specified for write processing) --Call Enqueue # setContentionActWait if you want to wait when resource contention occurs. --Enqueue with the Enqueue # obtain method. --Dequeue with the Enqueue # release method.

** Note 2) ** In the above sample, the scope is set to ** Enqueue.ISGENQ_SCOPE_SYSTEM ** in order to run as a standalone Java application, but if you want to control conflicts between multiple threads in the same JVM in the WAS for z / OS environment, * Specify * Enqueue.ISGENQ_STEP ** for the scope.

** Note 3) ** For more information on the API, see [Reference Link 2](https://www.ibm.com/support/knowledgecenter/SSYKE2_8.0.0/com.ibm.java.zsecurity.api.80.doc/com.ibm.jzos/com See /ibm/jzos/Enqueue.html). For system services called by the API, see Reference Link 3. .htm), reference link 4, [reference link 5] See (https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.3.0/com.ibm.zos.v2r3.ieaa200/isgenq.htm).

Reference link

1. JZOS Batch Launcher and Toolkit https://www.ibm.com/support/knowledgecenter/SSYKE2_8.0.0/com.ibm.java.zsecurity.80.doc/zsecurity-component/jzos.html

2. JZOS API (Javadoc) http://www.ibm.com/support/knowledgecenter/SSYKE2_8.0.0/com.ibm.java.zsecurity.api.80.doc/com.ibm.jzos/index.htm

** 3. Global resource sequentialization ** https://www.ibm.com/support/knowledgecenter/ja/SSLTBW_2.3.0/com.ibm.zos.v2r3.ieag400/comm.htm

** 4. ISGENQ — Global Resource Sequential ENQ Service ** https://www.ibm.com/support/knowledgecenter/ja/SSLTBW_2.3.0/com.ibm.zos.v2r3.ieaa900/isgenq.htm

** 5. ISGENQ — Global resource serialization ENQ service (English page above) ** https://www.ibm.com/support/knowledgecenter/ja/SSLTBW_2.3.0/com.ibm.zos.v2r3.ieaa200/isgenq.htm

** 6. Try accessing the dataset from Java using JZOS ** https://qiita.com/tsunogai/items/8f75ce6a2f52c6c61ff5

Recommended Posts

Achieve exclusive control of datasets in a multithreaded Java environment (WAS for z / OS environment) on IBM mainframes
[For beginners] Until building a Web application development environment using Java on Mac OS
Notes on signal control in Java