VSAM file locking when writing to it using Java JD

2019-09-19 11:10发布

问题:

This is my first time trying to read and write to a VSAM file. What I did was:

  1. Created a Map for the File using VSE Navigator
  2. Added the Java beans VSE Connector library to my eclipse Java project
  3. Use the code show below to Write and Read to the KSDS file.

Reading the file is not a problem but when I tried to write to the file it only works if I go on the mainframe and close the File before running my java program but it locks the file for like an hour. You cannot open the file on the mainframe or do anything to it.

Anybody can help with this problem. Is there a special setting that I need to set up for the file on the mainframe ? Why do you first need to close the file on CICS to be able to write to it ? And why does it locks the file after writing to it ?

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.*;
public class testVSAM {

public static void main(String argv[]){
    Integer test = Integer.valueOf(2893);
    String vsamCatalog = "VSESP.USER.CATALOG";
    String FlightCluster = "FLIGHT.ORDERING.FLIGHTS";       
    String FlightMapName = "FLIGHT.TEST2.MAP";
    try{

        String ipAddr = "10.1.1.1";                     
        String userID = "USER1";            
        String password = "PASSWORD"; 

        java.sql.Connection jdbcCon;            
        java.sql.Driver jdbcDriver = (java.sql.Driver) Class.forName(
        "com.ibm.vse.jdbc.VsamJdbcDriver").newInstance();
        // Build the URL to use to connect
        String url = "jdbc:vsam:"+ipAddr;
        // Assign properties for the driver
        java.util.Properties prop = new java.util.Properties();
        prop.put("port", test);
        prop.put("user", userID);
        prop.put("password", password);
        // Connect to the driver
        jdbcCon = DriverManager.getConnection(url,prop);

        try {
            java.sql.PreparedStatement pstmt = jdbcCon.prepareStatement(
            "INSERT INTO "+vsamCatalog+"\\"+FlightCluster+"\\"+FlightMapName+
            " (RS_SERIAL1,RS_SERIAL2,RS_QTY1,RS_QTY2,RS_UPDATE,RS_UPTIME,RS_EMPNO,RS_PRINTFLAG,"+
            "RS_PART_S,RS_PART_IN_A_P,RS_FILLER)"+" VALUES(?,?,?,?,?,?,?,?,?,?,?)");
            //pstmt.setString(1, "12345678901234567890123003");
            pstmt.setString(1, "1234567890");
            pstmt.setString(2,"1234567890123");
            pstmt.setInt(3,00);
            pstmt.setInt(4,003);
            pstmt.setString(5,"151209");
            pstmt.setString(6, "094435");
            pstmt.setString(7,"09932");
            pstmt.setString(8,"P");
            pstmt.setString(9,"Y");
            pstmt.setString(10,"Y");
            pstmt.setString(11," ");
            // Execute the query
            int num = pstmt.executeUpdate();
            System.out.println(num);
            pstmt.close();

            }
            catch (SQLException t)
            {

                System.out.println(t.toString());                       
            } 

        try
        {
        // Get a statement
        java.sql.Statement stmt = jdbcCon.createStatement();
        // Execute the query ...
        java.sql.ResultSet rs = stmt.executeQuery(
        "SELECT * FROM "+vsamCatalog+"\\"+FlightCluster+"\\"+FlightMapName);

        while (rs.next())
        {                                           
        System.out.println(rs.getString("RS_SERIAL1") +  " " + rs.getString("RS_SERIAL2")+  " " + rs.getString("RS_UPTIME")+ " " + rs.getString("RS_UPDATE"));
        }
        rs.close();
        stmt.close();
        }
        catch (SQLException t)
        {

        } 
    }
    catch (Exception e)
    {
        // do something appropriate with the exception, *at least*:
        e.printStackTrace();
    } 

}

}

Note: the OS is z/VSE

回答1:

The short answer to your original question is that KSDS VSAM is not a DBMS.

As you have discovered, you can define the VSAM file such that you can update it both from batch and from CICS, but as @BillWoodger points out, you must serialize your updates yourself.

Another approach would be to do all updates from the CICS region, and have your Java application send a REST or SOAP or MQ message to CICS to request its updates. This does require there be a CICS program to catch the requests from the Java application and perform the updates.



回答2:

The IBM Mainframe under z/VSE has different partitions that run different jobs. For example partition F7 CICS, partition F8 Batch Jobs, ETC.

When you define a new VSAM file you have to set the SHAREOPTIONS of the file. When I define the file I set the SHAREOPTIONS (2 3). 2 Means that only one partition can write to the file.

So when the batch program (in a different partition to the CICS partition) which is called from Java was trying to write to the file it was not able to write to the file unless I close the file in CICS first.

To fix it I REDEFINE the CICS file with SHAREOPTIONS (4 3). 4 Means that multiple partitions of the Mainframe can write to it. Fixing the problem

Below is a part of the definition code where you set the SHAREOPTION:

* $$ JOB JNM=DEFFI,CLASS=9,DISP=D,PRI=9         
* $$ LST CLASS=X,DISP=H,PRI=2,REMOTE=0,USER=JAVI
// JOB DEFFI                                    
// EXEC IDCAMS,SIZE=AUTO                                                         
DEFINE  CLUSTER -                            
          ( -                                
          NAME (FLIGHT.ORDERING.FLIGHTS) -  
          RECORDS (2000 1000) -              
          INDEXED -                          
          KEYS (26 0) -                      
          RECORDSIZE (128 128) -             
          SHAREOPTIONS (4 3) -               
          VOLUMES (SYSWKE) -                 
          ) -                                
          .
          .  
          .