cancel
Showing results for 
Search instead for 
Did you mean: 

Please validate JCA code to call BAPI

Former Member
0 Kudos

Here is an example of some code using JCA to connect to SAP. I was hoping that someone in the group may be able to answer the following questions:

1) Is it important to close the interaction and connection in the 'finally' block ? If I fail to close interactions and connections will I leave open cursors in SAP ...like oracle ?

2) What does the line: "RecordFactory recordFactory = connectionFactory.getRecordFactory();" do ?

3) I did not use the lines: connection.getLocalTransaction().begin(); and connection.getLocalTransaction().commit();

anywhere since I did not do any changes, is this correct reasoning ? ... I ask this because the sample code delivered with the JRA librairy includes these lines when reading from SAP.

4) It seems as if every time I want to call an RFC I have to actually make 2 calls to SAP. The first call to retrieve the input parameter structure, and the second to actually execute the call. If this is so, does anyone know how I may cache the input structures at the startup of my application ?

5) Any other comments/suggestions are welcome

Thanks in advance,

Mark

Here is the sample code: Example.java

/*********************************************************

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.sql.ResultSetMetaData;

import java.util.Properties;

import java.util.Vector;

import javax.resource.cci.Connection;

import javax.resource.cci.ConnectionFactory;

import javax.resource.cci.Interaction;

import javax.resource.cci.MappedRecord;

import javax.resource.cci.RecordFactory;

import javax.resource.cci.ResultSet;

import javax.resource.spi.ManagedConnectionFactory;

import com.sap.mw.jco.jra.JRA;

public class Example

{

public void exampleTest()

{

Connection connection = null;

Interaction interaction = null;

try

{

// Get the properties for the logon

Properties loginProps = new Properties();

loginProps.load(new FileInputStream("logon.properties"));

// "logon.properties" file looks like

//

// jco.client.client=331

// jco.client.user=xxxxxx

// jco.client.passwd=yyyyyy

// jco.client.ashost=host

// jco.client.sysnr=56

// Open the connection

ManagedConnectionFactory managedConnectionFactory = new JRA.ManagedConnectionFactoryImpl(loginProps);

ConnectionFactory connectionFactory = (ConnectionFactory)managedConnectionFactory.createConnectionFactory();

connection = connectionFactory.getConnection();

// I don't know what this line does

RecordFactory recordFactory = connectionFactory.getRecordFactory();

// Makes first call to SAP, requesting the structure of the input parameters

MappedRecord request = recordFactory.createMappedRecord("RFC_READ_TABLE");

// Set the input parameter

request.put("QUERY_TABLE","T001");

// Makes second call to SAP, executing the RFC

interaction = connection.createInteraction();

MappedRecord response = (MappedRecord)interaction.execute(null,request);

// Get the results

//ResultSet rs1 = (ResultSet)response.get("OPTIONS");

ResultSet rs2 = (ResultSet)response.get("FIELDS");

ResultSet rs3 = (ResultSet)response.get("DATA");

//System.out.println( debugPrintReturnInfo(rs1) );

System.out.println( debugPrintReturnInfo(rs2) );

System.out.println( debugPrintReturnInfo(rs3) );

}

catch(Throwable t)

{

t.printStackTrace();

}

finally

{

// Close the interaction

try{ if ( interaction != null ) interaction.close(); } catch(Throwable t){}

// Close the connection

try{ if ( connection != null )connection.close(); } catch(Throwable t){}

}

}

public String debugPrintReturnInfo(ResultSet unResultSet)

{

String ln = System.getProperty("line.separator");

StringBuffer buf = new StringBuffer();

try

{

ResultSetMetaData rsmd = unResultSet.getMetaData();

String[] colNames = new String[rsmd.getColumnCount()];

String[] colTypes = new String[rsmd.getColumnCount()];

for (int i = 1; i <= rsmd.getColumnCount(); i++)

{

String colName = rsmd.getColumnName(i);

colTypes[i-1] = rsmd.getColumnTypeName(i);

colNames[i-1] = colName;

}

while( unResultSet.next() )

{

for (int j = 0; j < colNames.length; j++)

{

buf.append( colNames[j] + " = " + unResultSet.getObject(colNames[j])+ ": "+ colTypes[j] + ln);

}

buf.append(ln);

}

}

catch(Throwable t)

{

t.printStackTrace();

}

return buf.toString();

}

public static void main(String[] args)

{

Example anExample = new Example();

try

{

anExample.exampleTest();

}

catch(Throwable t)

{

t.printStackTrace();

}

}

}

Message was edited by: Mark Côté

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

Hi Mark,

I want to use JCA for accesing BAPI from IBM Websphere. Can you please guide.. step by step..

Can you pls send me the jar files and JCA rar file u have used to georgeinnet@gmail.com

Regards

George

Former Member
0 Kudos

Hi Mark,

I´m a newbie in SAP, but I'm wondering why you did NOT use a lookup for the ConnectionFactory of the Adapter in your example.

Is it because of the authentication ?

I'm using WebSphere too, and is the JAAS Authentication Mecanism not working ? Is there in SAP JRA a ConnectionSpec for that?

thanks a lot,

Sergio

Benny
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi

> 1) Is it important to close the interaction and

> connection in the 'finally' block ? If I fail to

> close interactions and connections will I leave open

> cursors in SAP ...like oracle ?

The ABAP part is NOT a database. Therefore there are no cursors or anything like. This is business content and if you modify stuff you shouldn't via a special table as your changes may require certain actions by the software.

>

> 2) What does the line: "RecordFactory recordFactory =

> connectionFactory.getRecordFactory();" do ?

This is part of the JCA standard and it's API. I suggest reading about the standard at java.sun.com

> 3) I did not use the lines:

> connection.getLocalTransaction().begin(); and

> connection.getLocalTransaction().commit();

> anywhere since I did not do any changes, is this

> correct reasoning ? ... I ask this because the sample

> code delivered with the JRA librairy includes these

> lines when reading from SAP.

So far the ABAP world does follow the database paradigm: If there is no write, there is no commit needed.

> 4) It seems as if every time I want to call an RFC I

> have to actually make 2 calls to SAP. The first call

> to retrieve the input parameter structure, and the

> second to actually execute the call. If this is so,

> does anyone know how I may cache the input structures

> at the startup of my application ?

This call is not quite typical for a ABAP call. Usually you would call BAPI's, which means you know before the call what the structures are.

Regards,

Benny