Thursday, 26 July 2007

Access a Data Source from OC4J remotely from a java client

I needed to access an data source in an OPMN managed OC4J instance remotely from a java client. My data source was in the release of Oracle Application Server and my client was JDeveloper Here is how I got this to work.

1. New Empty project in JDeveloper
2. Add the following libraries, which will ensure all the required jars needed are added to the classpath:

  • Oracle JDBC
  • JSP Runtime
  • Apache Ant
3. Create a client as follows, the connection is made with a provider URL using opmn:ormi format as shown below:
package pas.test;

import java.sql.Connection;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;

import javax.sql.DataSource;

public class AccessDataSource10132

public static void main(String[] args)
throws Exception
Connection conn = null;
Hashtable env = new Hashtable();
env.put(Context.SECURITY_PRINCIPAL, "oc4jadmin");
env.put(Context.SECURITY_CREDENTIALS, "welcome1");

InitialContext ic = new InitialContext(env);

DataSource ds = (DataSource) ic.lookup("jdbc/scottDS");
conn = ds.getConnection();
System.out.println("Auto commit is -> " + conn.getAutoCommit());

System.out.println("all done..");

The output is as follows:

Auto commit is -> true
all done..


Pas Apicella said...

I noticed that if your data source is setup to use FCF (Fast Connection Failover) and a Connection Caching that you also need ons.jar added to the classpath for the client.

Ashish said...

I tried the way you described in the post. Iam trying to lookup a datasource from a java project in Jdeveloper. The provider url in my case is - ormi://localhost:12401/default. Iam getting an error -

Aug 27, 2008 5:39:30 PM oracle.j2ee.rmi.RMIMessages EXCEPTION_ORIGINATES_FROM_THE_REMOTE_SERVER
WARNING: Exception returned by remote server: {0} oracle.oc4j.sql.spi.ManagedConnectionFactoryImpl; local class incompatible: stream classdesc serialVersionUID = 5036353448034635021, local class serialVersionUID = -7010736017799296921

It seems that this is because of version difference of class. But not sure about this.

Pas Apicella said...

This is because of incompatible jar files between the client and server.

So put simply if your have an OAS server simply use JDeveloper, or if you have OAS use JDeveloper and so on.

Another option is to use the JAR files from the OAS server itself that will ensure you have the same JAR files on the server and the client. That will work if your using eclipse for example.