cancel
Showing results for 
Search instead for 
Did you mean: 

Java Connector - JCo clear connections

Former Member
0 Kudos

Hi SAP developers.

I'm writing a web application, which using the Java Connector (version 3).

It was quite easy to get running(although I prefer the generated code from the SAP .NET Connector).

But I can't quite work out, how the connection gets created, how the example program really works. I mean, I get it and similar programs working, but I don't understand why they work (which I consider to be very important).

How does JCoDestinationManager.getDestination(ABAP_AS_POOLED) know to look in "ABAP_AS_WITH_POOL.jcoDestination"? (I know how the file gets written obviously).

Is there a way of creating a connection/connection pool without creating all of these files?

For example, parts of the example StepByStepClient.java from the JCo ver 3

import com.sap.conn.jco.JCoField;

import com.sap.conn.jco.JCoFunction;

import com.sap.conn.jco.JCoFunctionTemplate;

import com.sap.conn.jco.JCoStructure;

import com.sap.conn.jco.JCoTable;

import com.sap.conn.jco.ext.DestinationDataProvider;

public class StepByStepClient

{

static String ABAP_AS = "ABAP_AS_WITHOUT_POOL";

static String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL";

static String ABAP_MS = "ABAP_MS_WITHOUT_POOL";

static

{

Properties connectProperties = new Properties();

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

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

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

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

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

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

createDataFile(ABAP_AS, "jcoDestination", connectProperties);

connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3");

connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "10");

createDataFile(ABAP_AS_POOLED, "jcoDestination", connectProperties);

connectProperties.clear();

connectProperties.setProperty(DestinationDataProvider.JCO_MSHOST, "binmain");

connectProperties.setProperty(DestinationDataProvider.JCO_R3NAME, "BIN");

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

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

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

connectProperties.setProperty(DestinationDataProvider.JCO_GROUP, "PUBLIC");

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

createDataFile(ABAP_MS, "jcoDestination", connectProperties);

}

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 step1Connect() throws JCoException

{

JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS);

System.out.println("Attributes:");

System.out.println(destination.getAttributes());

System.out.println();

destination = JCoDestinationManager.getDestination(ABAP_MS);

System.out.println("Attributes:");

System.out.println(destination.getAttributes());

System.out.println();

}

public static void step2ConnectUsingPool() throws JCoException

{

JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);

destination.ping();

System.out.println("Attributes:");

System.out.println(destination.getAttributes());

System.out.println();

}

//.....some methods

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

{

step1Connect();

step2ConnectUsingPool();

//step3SimpleCall();

//step4WorkWithTable();

//step4SimpleStatefulCalls();

}

}

I would be really greatful if anyone could provide me with some insight.

Thanks

Martin

Accepted Solutions (0)

Answers (5)

Answers (5)

Former Member
0 Kudos

Hi

Re : Question 1,

the static String ABAP_AS_POOLED is defined as = "ABAP_AS_WITH_POOL", not = "ABAP_AS_WITH_POOL.jcoDestination";

I find that a little odd we don't give the full qualified name to the property file...JCo just "magically" adds the extension ".jcoDestination". Perhpas this is simply a default extension.

Re: Question 2,

In Version 2, there seems to be some additional methods / a different paradigma. It seems like Version 3 is completely dependant on a properties file.

JCo.createClientPool doesn't seem to exist any more This would have beeen my prefered way to create a connection, but perhaps this is simply because I haven't really understood version 3? (I mean - it should be better, right?)

I mean, why should I have to create a properties file? Thats just messy. Since I am starting with JCo for the firstttime, I would normally want to have the newest version, but maybe this time it makes sense to take the old version 2 instead.

This document from SAP explains that differences quite well.

http://help.sap.com/saphelp_nwmobile71/helpdata/en/47/00f948f8ba21c9e10000000a114a6b/content.htm

So, here is what I think I would need to do,

on entering my own method, get connection....

-read the properities file - check if they are what I desire.

-If they do not exist / have changed, then replace the properties file with the new settings

-call JCoDestinationManager.getDestination(MY_KEY)

-somehow by SAP magic, SAP notices that the properties file has changed for MY_KEY, and recreates its connection as accessed by the key?. SAP then provides a JCoDestination for my usage

-call whatever methods are required.

-I don't need to return the JCoDesination like it would be in Version 3 or in File IO or JDBC or......

Have I misunderstood the new API? Those folks at SAP are normally very smart.

Thanks

Martin

Former Member
0 Kudos

Hi Martin,

I don't think "jcoDestination" is a default extension. If it were, it would/should have been declared as a static variable in some Interface/class. If they have left it as something which the user can configure or change himself, how are they to know what string the user will use?

If they always require a file with the suffix ".jcoDestination", I think a standard method would have been offered where you just set the path and properties and the file would then be created automatically.

I am also just guessing here. I havent used the version 3. Still am on 2.1.8 and find it still good

The link that you mention was interesting, but I dont think the principle of loading an external properties file changes. You can always say that you expect a connection.jcoDestination (in case getDestination() only expects a file with the .jcoDestination extension) file with the properties specified in it and just use the code example stated at the link. Just load your destination as "connection" and it should work (I hope )

T00th

Former Member
0 Kudos

Well a little late than never.

The problem is, one needs to implement a DestinationDataProvider ....the default one from SAP seaches for the jcoDestination files.

Thanks for your help anyway.

Former Member
0 Kudos

Hi Martin,

To answer your first question ("How does JCoDestinationManager.getDestination(ABAP_AS_POOLED) know to look in "ABAP_AS_WITH_POOL.jcoDestination"?) -

The name ABAP_AS_WITH_POOL is assigned to the static place holder "ABAP_AS_POOLED". That means, you can reference to the String ABAP_AS_WITH_POOLED through ABAP_AS_POOLED.

Therefore, the statement "JCoDestinationManager.getDestination(ABAP_AS_POOLED)" looks for the destination defined under the name ABAP_AS_POOLED. It then takes the connection properties you have there and builds a connection.

Now your second question ("Is there a way of creating a connection/connection pool without creating all of these files?") -

Yes you can :). I generally have an external properites file (for e.g client.properties) where I enter my connection information. I read this file at startup and use the connection parameters entered there by the user to create my connection.

In this case I create my own connection pool based on the properties I read from the file and then just build a connection when I want to. This way I don't need to change my code everytime the connection properties are changed.

I currently also use the "old" JCo (version 2.1.8) so I am not too familiar with the v3.x API. You will need to look into the documentation to see how you can create a connection pool.

T00th

Former Member
0 Kudos

Hi Ayyapparaj,

If I understand your code correctly, the Netweaver admin to configure a connection pool named WD_MODELDATA_DEST, thats quite cool.

Unfortunately my application runs in the Tomcat application server, not in the SAP NAS, so that means I can't use Netweaver functionality???

Thanks

Martin

Former Member
0 Kudos

Hi,

If you are creating your web application for the SAP WAS then instead of creating files you can create the destination using NWA(Netweaver administration) and access that using your code as follows

Following is the sample code



static String ABAP_AS = "WD_MODELDATA_DEST"; // Created using Destination all credentials and system details are provided as part of it, easy to maintain when moving the code base from PROD->QA->TEST

PrintWriter out = response.getWriter();
		 JCoDestination destination;
		try {
			destination = JCoDestinationManager.getDestination(ABAP_AS);
		    out.println("Attributes:");
		    out.println(destination.getAttributes());
		    out.println();
		} catch (JCoException e) {
			e.printStackTrace();
			out.println(e.getMessage());
		}

Regards

Ayyapparaj

ravindra_bollapalli2
Active Contributor
0 Kudos
Former Member
0 Kudos

Hi Bvr.

I had a look at your links, the first two especially were quite good (but perhaps also a little bit outdated?)

I think the Problem is, the Java Connector in version 3, doesn't seem to have the methods

JCo.addClientPool / JCo.removeClientPool....

I guess they must have moved these methods somewhere??? I'll have a look later and post back here. (Right now someone else is using my development PC).

Thanks for your help, I think it is a good pointer in the right direction.

Martin