cancel
Showing results for 
Search instead for 
Did you mean: 

Establishing a connection via Class

Former Member
0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

second

public class bapi_reqgetitem {

private static SapConnection sapConnection;

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

sapConnection = new SapConnection();

//sapConnection.con().ping();

//JCoDestination sapConnection = con.JCoDestinationManager.getDestination("ABAP_AS");

//sapConnection.ping();

//System.out.println("ABAP_AS destination is ok");

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

if(function == null)

throw new RuntimeException("BAPI_COMPANYCODE_GETLIST not found in SAP.");

//function.getImportParameterList().setValue("MATERIALDOCUMENT","5000013002");

//function.getExportParameterList().setActive("PRHEADER",false);

//function.getImportParameterList().setValue("NUMBER","0010015263");

//function.getImportParameterList().setValue("ITEMS_FOR_RELEASE", "X");

//System.out.println(function.getImportParameterList());

function.getImportParameterList().setValue("PLANT","1000");

function.getImportParameterList().setValue("PARTIALLY_ORDERED_ITEMS","X");

function.getImportParameterList().setValue("CLOSED_ITEMS","X");

try

{

function.execute(sapConnection.con());

}

catch(AbapException e)

{

System.out.println(e.toString());

return;

}

//System.out.println(function.getFunctionTemplate());

JCoTable codes = function.getTableParameterList().getTable("REQUISITION_ITEMS");

//System.out.println(codes.getMetaData());

System.out.println(codes.getNumRows());

for (int i = 0; i < codes.getNumRows(); i++)

{

codes.setRow(i);

System.out.println(codes.getString("MATERIAL"));

}/**/

}

}

The error looks so:



Exception in thread "main" java.lang.IllegalStateException: DestinationDataProvider already registered [SapConnection$MyDestinationDataProvider]

	at com.sap.conn.jco.rt.RuntimeEnvironment.setDestinationDataProvider(RuntimeEnvironment.java:132)

	at com.sap.conn.jco.ext.Environment.registerDestinationDataProvider(Environment.java:216)

	at SapConnection.con(SapConnection.java:66)

	at bapi_reqgetitem.main(bapi_reqgetitem.java:37)

I know what is mean, but don't know how to solve this problem?

Can someone help me?

Thnaks

Igor

Former Member
0 Kudos

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);

Former Member
0 Kudos

Thank you. The last solution, was of course the best.

I have just one more question, how can I make a logoff? I mean with your solution , I can log on and how can I kill a connection?

Thanks,

Igor

Former Member
0 Kudos

The short answer is: You don't. With JCo2, you were responsible to open and close the connection yourself. With JCo3, this is handled transparently by the API.

You don't need to close the connection. However, if you want to keep a specific connection open for several calls, you can explicitly keep the connection open for that purpose (if you want to keep the context). Please check the documentation of the methods in JCoContext for more information on that.

Other than that, you don't need to worry about that. You could also check out [this document|http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/6080c13b-e2d1-2c10-5f88-bd5a9e35d726?quicklink=index&overridelayout=true] for more information, especially for multi-threaded environments.

Former Member
0 Kudos

Thank you!

Answers (0)