Sharing Database Connections Between Java and COBOL

COBOL and JDBC can use a common database connection. To achieve this, use a combination of EXEC SQL statements, depending on how the connection is opened.
When Java opens the connection
Connection guidelines
  1. Pass the connection object to COBOL.
  2. Use EXEC SQL BIND CONNECTION to establish the connection.
  3. When done using the connection, use EXEC SQL UNBIND CONNECTION if Java still requires access to it, or use EXEC SQL DISCONNECT if Java no longer requires access.
Example
Java code establishes a JDBC connection, and passes the connection object to the DoCOBOLSQL method of the COBOLSQL class, which binds to the connection, executes SQL, and then unbinds the connection, enabling Java to continue using the JDBC connection for further processing:
  • COBOLSQL tb = new COBOLSQL();
  • Context ctx = new InitialContext();
  • DataSource ds = (DataSource)ctx.lookup("mfdb");
  • Connection con = ds.getConnection("sa","password");
  • tb.DoCOBOLSQL(con);
  • ... (other JDBC access)
COBOL COBOLSQL class and DoCOBOLSQL method:
       class-id COBOLSQL public.
         working-storage section.   
         method-id DoCOBOLSQL.
             local-storage section.
                exec sql include sqlca end-exec.
             linkage section.
             01  con         type java.sql.Connection.
             procedure division using by value con.       
                exec sql bind connection to :con end-exec
                if sqlcode < 0
                    display "FAIL: Bind Connection " sqlcode 
                    display sqlerrmc
                end-if
                exec sql … (other SQL) end-exec
                exec sql unbind connection end-exec

                goback.
         end method. 
Note: Methods in the COBOL class are not limited as shown in this example. For instance, you could alternatively have a BIND method, a DoSQL method, and an UNBIND method.
When COBOL opens the connection
Connection guidelines
  1. Use EXEC SQL GET CONNECTION to get a connection object to pass to Java.
  2. When done using the connection, use EXEC SQL UNBIND CONNECTION if Java still requires access to it, or use EXEC SQL DISCONNECT if Java no longer requires access.
Example
JAVA code calls the DoCOBOLSQL method of the COBOLSQL class, which connects to the database, executes SQL, gets the connection and returns it to Java, then unbinds the connection, enabling Java to continue using the JDBC connection for further processing:
  • COBOLSQL tb = new COBOLSQL();
  • Connection con = tb.DoCOBOLSQL();
  • Statement stat = con.createStatement();
  • ResultSet r = stat.executeQuery("select RegionDescription from Region where RegionID = 1");
  • r.next();

COBOL COBOLSQL class and DoCOBOLSQL method:

       class-id COBOLSQL public.
         working-storage section.   
         method-id DoCOBOLSQL.
             local-storage section.
                exec sql include sqlca end-exec.
             linkage section.
             01  con         type java.sql.Connection.
             procedure division returning con.       
                EXEC SQL CONNECT TO :dbname USER :usr END-EXEC
                exec sql … (other SQL) end-exec
                exec sql get connection into :con end-exec
                exec sql unbind connection end-exec

                goback.
         end method. 
Note: Methods in the COBOL class are not limited as shown in this example.
When Run-Unit is terminated
Within a run-unit, EXEC SQL UNBIND CONNECTION closes cursors but does not close the connection. However, if a run-unit terminates and UNBIND CONNECTION has not been called, OpenESQL closes both cursors and the connection.