views:

1542

answers:

5

I'm working with a legacy WebLogic application that contains a web-service application and a standalone, command-line application. Both need access to a common database and I would like to try and get the command-line application use a pooled connection to the web-server's JDBC connection. (The standalone application can only be run when the server is active and both will be run on the same physical machine.)

I've been trying to use a JNDI lookup to the JDBC driver as follows:

try {
    Context ctx = null;
    Hashtable ht = new Hashtable();
    ht.put(Context.INITIAL_CONTEXT_FACTORY,
           "weblogic.jndi.WLInitialContextFactory");
    ht.put(Context.PROVIDER_URL, "t3://localhost:7001");

    ctx = new InitialContext(ht);
    DataSource ds = (DataSource) ctx.lookup ("dbOracle");
    Connection conn = null;
    conn = ds.getConnection();  // <-- Exception raised here
    // conn = ds.getConnection(username, password); // (Also fails)

    // ...

} catch (Exception e) {
    // Handle exception...
}

I've confirmed the JNDI name is correct. I am able to connect to the database with other web-applications but my standalone application continues to have difficulties. - I got the idea for this from a WebLogic app note.

Any ideas on what I have overlooked?

EDIT 1.1: I'm seeing a "java.lang.ClassCastException: java.lang.Object" exception.

EDIT 2: When I perform the following:

Object dsObj = ctx.lookup("dbOracle");
System.out.println("Obj was: " + dsObj.getClass().getName());

In the standalone-application, it reports:

"Obj was: weblogic.jdbc.common.internal._RemoteDataSource_Stub"

I attempted to test the same chunk of code (described in original question) in the web-application and was able to connect to the datasource (i.e. it seems to "work"). This working test reports:

"Obj was: weblogic.jdbc.common.internal.RmiDataSource"

Also, here's a stack-trace for when it's failing:

####<Apr 22, 2009 10:38:21 AM EDT> <Warning> <RMI> <mlbdev16> <cgServer> <[ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1240411101452> <BEA-080003> <RuntimeException thrown by rmi server: weblogic.jdbc.common.internal.RmiDataSource.getConnection()
 java.lang.ClassCastException: java.lang.Object.
java.lang.ClassCastException: java.lang.Object
    at weblogic.iiop.IIOPOutputStream.writeAny(IIOPOutputStream.java:1584)
    at weblogic.iiop.IIOPOutputStream.writeObject(IIOPOutputStream.java:2222)
    at weblogic.utils.io.ObjectStreamClass.writeFields(ObjectStreamClass.java:413)
    at weblogic.corba.utils.ValueHandlerImpl.writeValueData(ValueHandlerImpl.java:235)
    at weblogic.corba.utils.ValueHandlerImpl.writeValueData(ValueHandlerImpl.java:225)
    at weblogic.corba.utils.ValueHandlerImpl.writeValue(ValueHandlerImpl.java:182)
    at weblogic.iiop.IIOPOutputStream.write_value(IIOPOutputStream.java:1957)
    at weblogic.iiop.IIOPOutputStream.write_value(IIOPOutputStream.java:1992)
    at weblogic.iiop.IIOPOutputStream.writeObject(IIOPOutputStream.java:2253)
    at weblogic.jdbc.common.internal.RmiDataSource_WLSkel.invoke(Unknown Source)
    at weblogic.rmi.internal.BasicServerRef.invoke(BasicServerRef.java:589)
    at weblogic.rmi.cluster.ClusterableServerRef.invoke(ClusterableServerRef.java:224)
    at weblogic.rmi.internal.BasicServerRef$1.run(BasicServerRef.java:479)
    at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:363)
    at weblogic.security.service.SecurityManager.runAs(Unknown Source)
    at weblogic.rmi.internal.BasicServerRef.handleRequest(BasicServerRef.java:475)
    at weblogic.rmi.internal.BasicServerRef.access$300(BasicServerRef.java:59)
    at weblogic.rmi.internal.BasicServerRef$BasicExecuteRequest.run(BasicServerRef.java:1016)
    at weblogic.work.ExecuteThread.execute(ExecuteThread.java:200)
    at weblogic.work.ExecuteThread.run(ExecuteThread.java:172)
+1  A: 

Hard to tell what is going on with such limited information.

Did you set the application up as a thin client? When you are not operating within one of the JEE containers, the context and means of doing lookups does not work the same way (varies by vendor). You may want to research how to do lookups of resources with Weblogic from such a client, as well as how to set it up correctly.

Robin
I agree... we clearly need more data.
jsight
I have just added additional information. I hope it'll be helpful.
Nate
+1  A: 

Perhaps a classpath issue or even a jvm version thing?

KarlP
A: 

One possibility: you import incorrect "DataSource" class :-)

Try replacing this line of code:

DataSource ds = (DataSource) ctx.lookup ("dbOracle");

for this:

javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup ("dbOracle");

Just an idea, hope this will help you

SourceRebels
+1  A: 

It is possible that the JARs you have for Oracle driver in the client JVM are different than the ones in the server. make sure the same Oracle JDBC driver jar is in both classpaths of the command-line tool and the weblogic webapp

good luck !

Oren Yosifon
+1  A: 

This exception looks like it is generated on the server side. The first thing I would do is verify the version of WebLogic Server and check the classpath of the client application. You need to make sure your client app has the same version of the WebLogic client jar files as the WebLogic Server. You are including weblogic client jar files in the classpath of your client, right? Since your are using WebLogic's RMI driver, I don't believe you need any Oracle jar files in the classpath of the client. Your client is essentially speaking RMI to the WebLogic Server. The WebLogic Server connection pool you configured knows how to speak to Oracle. It shouldn't matter what JDBC driver you use in your connection pool.

Gary
I was *not* including the WebLogic client jar files in the application's classpath. Thanks!
Nate
Yup, it isn't obvious by only looking at the import statements of your code that you need it. The compiler won't complain, but casting the object looked up will barf.
Gary