title: Application using Pooled JDBC Connections
body: <p>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.)</p>
  
  <p>I've been trying to use a JNDI lookup to the JDBC driver as follows:</p>
  
  <pre><code>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();  // &lt;-- Exception raised here
      // conn = ds.getConnection(username, password); // (Also fails)
  
      // ...
  
  } catch (Exception e) {
      // Handle exception...
  }
  </code></pre>
  
  <p>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 <a href="http://download.oracle.com/docs/cd/E12840_01/wls/docs103/jdbc/programming.html" rel="nofollow noreferrer">WebLogic app note</a>.</p>
  
  <p>Any ideas on what I have overlooked?</p>
  
  <p><strong>EDIT 1.1:</strong>  I'm seeing a "java.lang.ClassCastException: java.lang.Object" exception.</p>
  
  <p><strong>EDIT 2:</strong>  When I perform the following:</p>
  
  <pre><code>Object dsObj = ctx.lookup("dbOracle");
  System.out.println("Obj was: " + dsObj.getClass().getName());
  </code></pre>
  
  <p>In the standalone-application, it reports:</p>
  
  <pre><code>"Obj was: weblogic.jdbc.common.internal._RemoteDataSource_Stub"
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>"Obj was: weblogic.jdbc.common.internal.RmiDataSource"
  </code></pre>
  
  <p>Also, here's a stack-trace for when it's failing:</p>
  
  <pre><code>####&lt;Apr 22, 2009 10:38:21 AM EDT&gt; &lt;Warning&gt; &lt;RMI&gt; &lt;mlbdev16&gt; &lt;cgServer&gt; &lt;[ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)'&gt; &lt;&lt;WLS Kernel&gt;&gt; &lt;&gt; &lt;&gt; &lt;1240411101452&gt; &lt;BEA-080003&gt; &lt;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)
  </code></pre>