Pessimistic lock on NW ABAP from Java via JCo

I've checked SAP NetWeaver ABAP's pessimistic lock using stateful/stateless call from Java via JCo(SAP Java Connector).  When 

- How to create JCO and JDK environment is on [another article(Japanese)](https://qiita.com/FukuharaYohei/items/d1f367a0fb6e6670378f).
- How to create Eclipse and Jetty environment is on [another article(Japanese)](https://qiita.com/FukuharaYohei/items/03c41b58f8a2e2f30a39).

# Development Environment
- IDE: Eclipse(Oxygen.2 Release (4.7.2))
- JDK: Oracle JDK 1.8.0_152(**not SAP JDK**)
- JCo: SAP Java Connector 3.0.17

#A lock object on NW ABAP AS
Copied a SAP lock object "EBU_PARTNER". Just changed "Allow RFC".

![20.LockObject.JPG](https://qiita-image-store.s3.amazonaws.com/0/134273/36a7b22c-cb53-d895-fa99-2c0b7de82008.jpeg)


#Stateful Call
After calling lock module, there is a sleep time on java and I checked the lock is active.

```Java:StatefulLock.java
package test00;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;
 
import com.sap.conn.jco.AbapException;
import com.sap.conn.jco.JCoContext;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.ext.DestinationDataProvider;
import com.sap.conn.jco.JCoFunction;
 
public class StatefulLock {

    static String ABAP_AS = "ABAP_AS_WITHOUT_POOL_RSM";

//  SAP system information
    static
    {
        Properties connectProperties = new Properties();
        connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "XXX"); //host name
        connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR,  "00");
        connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "100");
        connectProperties.setProperty(DestinationDataProvider.JCO_USER,   "fukuhara");
        connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "XXX"); //password
        connectProperties.setProperty(DestinationDataProvider.JCO_LANG,   "en");
        createDataFile(ABAP_AS, "jcoDestination", connectProperties);
    }    

//  Create SAP system property file
    static void createDataFile(String name, String suffix, Properties properties)
    {
        File cfg = new File(name+"."+suffix);
        if(!cfg.exists())
        {
            try
            {
                FileOutputStream fos = new FileOutputStream(cfg, false);
                properties.store(fos, "for tests only !");
                fos.close();
            }
            catch (Exception e)
            {
                throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e);
            }
        }
    }
    
    public static void step3SimpleCall() throws JCoException
    {
    	// Set Destination
        JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS);
        
        // Set lock function module
        JCoFunction function = destination.getRepository().getFunction("ENQUEUE_EYBU_PARTNR");
        if(function == null)
            throw new RuntimeException("ENQUEUE_EYBU_PARTNR not found in SAP.");
        
        // Set Import Parameters
        function.getImportParameterList().setValue("PARTNER", "0000000101"); // Partner Number
        function.getImportParameterList().setValue("_SCOPE", "3");           // Lock scope
        
		try {
			
			// Begin Stateful Session
			JCoContext.begin(destination);
			try {
				
				// Call function module
				function.execute(destination);
		        try {
		        	
		        	// sleep for checking the lock
					System.out.println("Stop 15 senconds");
					Thread.sleep(15000);
					System.out.println("Restarted");
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			} catch (AbapException e) {
				System.out.println(e.toString());
				return;
			}
		} catch (JCoException ex) {
			
		} finally {
			
			// Finish stateful session
			JCoContext.end(destination);
		}
        
        System.out.println("ENQUEUE_EYBU_PARTNR finished:");
        System.out.println();


    }
    
	public static void main(String[] args) throws JCoException {
		step3SimpleCall();
	}

}

```

#Stateless call
After calling lock module, lock disappears, since SAP server doesn't have any states.

```Java:StatelessLock.java
package test00;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;
 
import com.sap.conn.jco.AbapException;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.ext.DestinationDataProvider;
import com.sap.conn.jco.JCoFunction;
 
public class StatelessLock {

    static String ABAP_AS = "ABAP_AS_WITHOUT_POOL_RSM";

//  SAP system information
    static
    {
        Properties connectProperties = new Properties();
        connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "XXX"); //host
        connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR,  "00");
        connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "100");
        connectProperties.setProperty(DestinationDataProvider.JCO_USER,   "fukuhara");
        connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "XXX"); 
        connectProperties.setProperty(DestinationDataProvider.JCO_LANG,   "en");
        createDataFile(ABAP_AS, "jcoDestination", connectProperties);
    }    

//  Create SAP system property file
    static void createDataFile(String name, String suffix, Properties properties)
    {
        File cfg = new File(name+"."+suffix);
        if(!cfg.exists())
        {
            try
            {
                FileOutputStream fos = new FileOutputStream(cfg, false);
                properties.store(fos, "for tests only !");
                fos.close();
            }
            catch (Exception e)
            {
                throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e);
            }
        }
    }
    
    public static void step3SimpleCall() throws JCoException
    {
    	// Set Destination
        JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS);
        
        // Set lock function module
        JCoFunction function = destination.getRepository().getFunction("ENQUEUE_EYBU_PARTNR");
        if(function == null)
            throw new RuntimeException("ENQUEUE_EYBU_PARTNR not found in SAP.");
        
        // Set Import Parameters
        function.getImportParameterList().setValue("PARTNER", "0000000101");
        function.getImportParameterList().setValue("_SCOPE", "3");
        
        try
        {
        	//Call lock function module
            function.execute(destination);
        }
        catch(AbapException e)
        {
            System.out.println(e.toString());
            return;
        }
        
        System.out.println("ENQUEUE_EYBU_PARTNR finished:");
        System.out.println();

        // Sleep for checking locks 
        try {
			System.out.println("Stop 15 senconds");
			Thread.sleep(15000);
			System.out.println("Restarted");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

    }
    
	public static void main(String[] args) throws JCoException {
		step3SimpleCall();
	}

}

```