cancel
Showing results for 
Search instead for 
Did you mean: 

Problem in Getting connection using MDM JAVA API..

Former Member
0 Kudos

Hi,

I am using MDMAPI 5.5 36.23 version to connect the MDM server.

I am getting error

Unable to create new connection.

com.sap.mdm.net.ConnectionException

while connecting.

Please tell me how i would have to give the "Connection String"

I just given localhost since server is running on localserver.

1.Can i give IP in the place of host name?

2.how do i know in which port the server is running??

Please help me.

Hari

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

since the error is a com.sap.mdm.net i assume you are using the new api style of connecting simple connection , get user session etc etc .

there is a great pdf that teaches how to use this . it was written by scott cairncross let me see if i find the link ...

here we go

https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/90590a43-3b82-2910-4882-9fe8b9be...

to connect you basicly have three steps :

1 ) get a simple connection or a conneciton pool instance



import com.sap.mdm.net.ConnectionException;
import com.sap.mdm.net.ConnectionPool;
import com.sap.mdm.net.ConnectionPoolFactory;
import com.sap.tc.logging.Location;

public class GetMDMConnection {
	String connectionTag = null;
	ConnectionPool conn = null ;
	Settings settings;

	/** reference to logging location 
	 * @see com.sap.tc.logging.Location */
	private static final Location trace = Location.getLocation(GetMDMConnection.class);


	public ConnectionPool getConnection() {
		settings = new Settings();
			try {
				conn = ConnectionPoolFactory.getInstance(“HOSTNAME”);
			} catch (ConnectionException e) {
				e.printStackTrace();
			}
			return conn;	
	}
}
 

2) get a authenticate user session


import com.sap.mdm.commands.AuthenticateUserSessionCommand;
import com.sap.mdm.commands.CommandException;
import com.sap.mdm.commands.CreateUserSessionCommand;
import com.sap.mdm.commands.DestroySessionCommand;
import com.sap.mdm.commands.GetRepositoryRegionListCommand;
import com.sap.mdm.data.RegionProperties;
import com.sap.mdm.net.ConnectionException;
import com.sap.mdm.net.ConnectionPool;
import com.sap.mdm.net.ConnectionPoolFactory;
import com.sap.mdm.server.DBMSType;
import com.sap.mdm.server.RepositoryIdentifier;
import com.sap.mdm.server.commands.GetServerVersionCommand;

public class GetUserSession {

	public String session;
	public ConnectionPool conn = null;
	public RepositoryIdentifier repId;
	private GetMDMConnection getConn = null;
	private Settings settings;

	public String Authenticate(ConnectionPool conn) {
		settings = new Settings();
		getConn = new GetMDMConnection();
		conn = getConn.getConnection();
		GetServerVersionCommand cmd = new GetServerVersionCommand(conn);
		try {
			cmd.execute();
			System.out.println(cmd.getVersion());
		} catch (CommandException e6) {
			e6.printStackTrace();
		}

		String repName = settings.getRepository();
		String dbName = settings.getDbServer();
		DBMSType dbType = settings.getDbmsType();
		//		  String repName = "REPOSITOR_NAME";
		//		  String dbName = "DB_NAME”;
		//		  DBMSType dbType =  DBMSType.IBM_DB2;

		repId = new RepositoryIdentifier(repName, dbName, dbType);

		RegionProperties dataRegion;

		GetRepositoryRegionListCommand getRepositoryRegionListCommand =
			new GetRepositoryRegionListCommand(conn);
		getRepositoryRegionListCommand.setRepositoryIdentifier(repId);
		try {
			getRepositoryRegionListCommand.execute();
		} catch (CommandException e1) {

			e1.printStackTrace();
		}
		RegionProperties regionProperties[] =
			getRepositoryRegionListCommand.getRegions();
		dataRegion = regionProperties[0];

		CreateUserSessionCommand createUserSessionCommand =
			new CreateUserSessionCommand(conn);
		createUserSessionCommand.setRepositoryIdentifier(repId);
		createUserSessionCommand.setDataRegion(dataRegion);
		try {
			createUserSessionCommand.execute();
		} catch (CommandException e2) {

			e2.printStackTrace();
		}

		session = createUserSessionCommand.getUserSession();

		AuthenticateUserSessionCommand authenticateUserSessionCommand =
			new AuthenticateUserSessionCommand(conn);
		authenticateUserSessionCommand.setSession(session);
		authenticateUserSessionCommand.setUserName(settings.getUsername());
		authenticateUserSessionCommand.setUserPassword(settings.getPassword());
		//authenticateUserSessionCommand.setUserName("Admin");
		//authenticateUserSessionCommand.setUserPassword("");

		try {
			authenticateUserSessionCommand.execute();
			return session;
		} catch (CommandException e3) {

			e3.printStackTrace();
			return null;
		}

	}

	public void destroy() throws CommandException {
		DestroySessionCommand destroySessionCommand =
			new DestroySessionCommand(conn);
		destroySessionCommand.setSession(session);
		destroySessionCommand.execute();
		session = null;
	}

	/**
	 * Tries to find a region in the MDM repository that matches the language and country of the
	 * locale in the settings.
	 * If not such region is found, the first available region from the repository is used.
	 * @return valid RegionProperties for the MDM repository
	 * @throws CommandException is propagated from the API
	 */
	private RegionProperties getRegion() throws CommandException {

		GetRepositoryRegionListCommand getRepositoryRegionListCommand =
			new GetRepositoryRegionListCommand(conn);
		getRepositoryRegionListCommand.setRepositoryIdentifier(repId);
		getRepositoryRegionListCommand.execute();

		RegionProperties regionProperties[] =
			getRepositoryRegionListCommand.getRegions();
		boolean found = false;
		int i = 0;

		do {
			if (regionProperties<i>.getLocale().equals("English"))
				found = true;
			else
				i++;
		} while (!found && i < regionProperties.length);

		if (found)
			return regionProperties<i>;
		return regionProperties[0];
	}

}

3) execute a command



import com.sap.mdm.commands.AuthenticateRepositorySessionCommand;
import com.sap.mdm.commands.CommandException;
import com.sap.mdm.commands.CreateRepositorySessionCommand;
import com.sap.mdm.net.ConnectionPool;
import com.sap.mdm.schema.FieldProperties;
import com.sap.mdm.schema.TableProperties;
import com.sap.mdm.schema.commands.GetFieldListCommand;
import com.sap.mdm.schema.commands.GetTableListCommand;
import com.sap.mdm.server.DBMSType;
import com.sap.mdm.server.RepositoryIdentifier;

public class GetTableList {

//		
		
	public static void main(String[] args) {
		GetMDMConnection getConn = null;
		ConnectionPool conn = null;
	  // 	GetUserSession getSession = null;
		String Session = null;
		
		getConn = new GetMDMConnection();
		conn = getConn.getConnection();		
	//	getSession = new GetUserSession();
	  //  Session =	getSession.Authenticate(conn);
		
	  String repName = "REPOSITORY_NAME";
	  String dbName = "DB_NAME";
	  DBMSType dbType =  DBMSType.IBM_DB2;
	  RepositoryIdentifier repId = new RepositoryIdentifier(repName , dbName, dbType);
	
	
CreateRepositorySessionCommand repSessComm = new CreateRepositorySessionCommand(conn);
repSessComm.setRepositoryIdentifier(repId);
try {
	repSessComm.execute();
	Session = repSessComm.getRepositorySession();
} catch (CommandException e1) {
	// TODO Auto-generated catch block
	e1.printStackTrace();
}

AuthenticateRepositorySessionCommand auRepComm = new AuthenticateRepositorySessionCommand(conn);
auRepComm.setSession(Session);
auRepComm.setUserName("Admin");
auRepComm.setUserPassword("");

try {
	auRepComm.execute();
	Session = auRepComm.getSession();
} catch (CommandException e2) {
	// TODO Auto-generated catch block
	e2.printStackTrace();
}


		
	//	TableId mainTAbleId  = new TableId("T1");
		
		GetTableListCommand tableComm = new GetTableListCommand(conn);
		try {
			tableComm.setSession(Session);
			tableComm.execute();
		} catch (CommandException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		TableProperties[] tabprop = tableComm.getTables();
		
		for(int i=0;i<tabprop.length;i++){
			System.out.print(tabprop<i>.getName().toString());
			System.out.println(" : " +tabprop<i>.getId().getString());
			//if(tabprop<i>.getId().getString().equals("T30")){
//			GetFieldListCommand fieldList = new GetFieldListCommand(conn);
//			fieldList.setTableId(tabprop<i>.getId());
//			fieldList.setSession(Session);
//			try {
//				fieldList.execute();
//				FieldProperties[] fieldProp = fieldList.getFields();
//							for (int j = 0; j < fieldProp.length; j++) {
//								System.err.print(fieldProp[j].getName().toString());
//								System.err.println(" : " +fieldProp[j].getId().getString());
//							}
//			} catch (CommandException e3) {
//				// TODO Auto-generated catch block
//				e3.printStackTrace();
//			}
		//	}
			
			
		}
		
	}
}

The last code has the code to authenticate also , too lazy to remove it , but if you want you can go ahead delete it and use the class from step 2 to return a authenticated session and you can go ahead and pass that to the command.

check out the javadocs for more commands

cheers,

Guy

Former Member
0 Kudos

The port is not exactly the port that the server is running on, it's referring to the port that the repository is running on. If you open up the console and look at each of the repositories you can see what port has been assigned to them.

Former Member
0 Kudos

Hi Hari,

Maybe this thread helps:

https://www.sdn.sap.com/irj/sdn/profile?userid=3459773

I haven't tried it myself, but also got no negative answer...

Please tell me whether it worked.

Kind regards,

Dennis