Connecting to the server

The EJBConnection class is used to connect to an EJB server and locate an EJB. It has four methods: ConnectToServer, DisconnectServer, Lookup, and GetEJBTransaction.

To establish a connection to the server, you need to execute the PowerScript statements required to perform these operations:

  1. Declare an instance of the EJBConnection class.

  2. Set properties for the EJBConnection object.

  3. Use the CREATE statement to instantiate the EJBConnection object.

  4. Invoke the ConnectToServer method to establish a connection to the server.

  5. Check for errors.

Class path requirements

To connect to the application server and create an EJB object, the system CLASSPATH environment variable or the classpath argument of createJavaVM must contain the location of the EJB stub files, either a directory or a JAR file. The application server you are using might also require that some classes or JAR files be available on the client computer and added to the class path. For more information, see The Java VM classpath in the development environment.

Setting the initial context

The string used to establish the initial context depends on the EJB server. The following table shows sample string values. See the documentation for your server for more information.

Server

INITIAL_CONTEXT_FACTORY value

WebLogic

weblogic.jndi.WLInitialContextFactory

WebSphere

com.ibm.websphere.naming.WsnInitialContextFactory


Example

The following script shows a connection to WebLogic. It sets connection properties to create an initial context, to identify the host name and port number of the server, and to identify the user ID and password.

Then, the script creates an instance of the EJBConnection object, invokes the ConnectToServer method to establish a connection to the server, and checks for errors:

ejbconnection conn
string properties[]

properties[1]="javax.naming.Context.INITIAL_CONTEXT_FACTORY= weblogic.jndi.WLInitialContextFactory"
properties[2]="javax.naming.Context.PROVIDER_URL=t3://svr1:7001"
properties[3]="javax.naming.Context.SECURITY_PRINCIPAL=myid"
properties[4]="javax.naming.Context.SECURITY_CREDENTIALS=mypass"

conn = CREATE ejbconnection
TRY
  conn.connectToServer(properties)
CATCH (exception e)
  MessageBox("exception", e.getmessage())
END TRY

Disconnecting from the server

When your application has finished using the EJB server, it should disconnect from the server:

conn.disconnectserver()