Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Establishing a connection via Class

Hi,

i have to classes. The first is for establishing a connection and the second for execute bapi.

Looks so:

first

public class SapConnection {

private static JCoDestination ABAP_AS;

static class MyDestinationDataProvider implements DestinationDataProvider

{

private DestinationDataEventListener eL;

private Properties ABAP_AS_properties;

public Properties getDestinationProperties(String destinationName)

{

if(destinationName.equals("ABAP_AS") && ABAP_AS_properties!=null)

return ABAP_AS_properties;

return null;

//alternatively throw runtime exception

//throw new RuntimeException("Destination " + destinationName + " is not available");

}

public void setDestinationDataEventListener(DestinationDataEventListener eventListener)

{

this.eL = eventListener;

}

public boolean supportsEvents()

{

return true;

}

void changePropertiesForABAP_AS(Properties properties)

{

if(properties==null)

{

ABAP_AS_properties = null;

eL.deleted("ABAP_AS");

}

else

{

if(ABAP_AS_properties==null || !ABAP_AS_properties.equals(properties))

{

ABAP_AS_properties = properties;

eL.updated("ABAP_AS");

}

}

}

}

public JCoDestination con() throws JCoException{

Properties connectProperties = new Properties();

connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "xxx");

connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "x");

connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "xx");

connectProperties.setProperty(DestinationDataProvider.JCO_USER, "xxx");

connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "xxx");

connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "xx");

MyDestinationDataProvider myProvider = new MyDestinationDataProvider();

com.sap.conn.jco.ext.Environment.registerDestinationDataProvider(myProvider);

myProvider.changePropertiesForABAP_AS(connectProperties);

ABAP_AS = JCoDestinationManager.getDestination("ABAP_AS");

return ABAP_AS;

}

}

Edited by: igorechek on Jun 9, 2011 10:27 PM

Former Member
Former Member replied

In your class "bapi_reqgetitem" you have at least two calls to con:

first:

JCoFunction function = sapConnection.con().getRepository().getFunction("BAPI_REQUISITION_GETITEMS");

second:

sapConnection.con()

It fails upon the second call since you try to register a destination data provider for the second time. There are several ways around that. The quick and dirty solution is to exchange the line in your first class

com.sap.conn.jco.ext.Environment.registerDestinationDataProvider(myProvider);

with

if (!com.sap.conn.jco.ext.Environment.isDestinationDataProviderRegistered()) {
		com.sap.conn.jco.ext.Environment.registerDestinationDataProvider(myProvider);
		}

A better solution would be of course to take the JCoDestination that you get as a returned instance from sapConnection.con() and work with that. It would look something like that:

JCoDestination jcoDestination = sapConnection.con()
(...)
JCoFunction function = jcoDestination.getRepository().getFunction("BAPI_REQUISITION_GETITEMS");
(...)
function.execute(jcoConnection);

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question