5

So I have a MySQL database, and I have a datasource on a local instance of WebLogic which is connected to that database. I am trying to write some client code which will simply connect and query. I am having issues with obtaining a connection from the datasource. Here's my code thus far. I am running WebLogic 12c.

    import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class ConnectionTest {

    public static void main(String... args) {
        ConnectionTest tCon = new ConnectionTest();
        tCon.TestConnection();

    }

    public void TestConnection() {
        Context ctx = null;
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {
            Hashtable<String, String> props = new Hashtable<String, String>();

            props.put("java.naming.factory.initial",
                    "weblogic.jndi.WLInitialContextFactory");
            props.put("java.naming.provider.url", "t3://localhost:7001");
            props.put("java.naming.security.principal", "weblogic");
            props.put("java.naming.security.credentials", "welcome1");
            ctx = new InitialContext(props);
            DataSource ds = (DataSource) ctx.lookup("RegexDB");
            System.out.println(ds);
            DAO dao = new DAO();
            conn = ds.getConnection();
            stmt = conn.createStatement();
            stmt.execute("select * from regular_ex");
            rs = stmt.getResultSet();
            ArrayList<HashMap<String, Object>> results = dao
                    .resultSetToArrayList(rs);
            dao.printArrayList(results);
            stmt.close();
            conn.close();
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (stmt != null)
                    stmt.close();
                if (conn != null)
                    conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}

This fails at ds.getConnection() with the following exception:

java.lang.ClassCastException: weblogic.jdbc.common.internal.ConnectionEnv cannot be cast to java.io.Serializable
    at weblogic.iiop.IIOPOutputStream.writeObject(IIOPOutputStream.java:2285)
    at weblogic.utils.io.ObjectStreamClass.writeFields(ObjectStreamClass.java:414)
    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:1983)
    at weblogic.iiop.IIOPOutputStream.write_value(IIOPOutputStream.java:2021)
    at weblogic.iiop.IIOPOutputStream.writeObject(IIOPOutputStream.java:2285)
    at weblogic.jdbc.common.internal.RmiDataSource_WLSkel.invoke(Unknown Source)
    at weblogic.rmi.internal.BasicServerRef.invoke(BasicServerRef.java:695)
    at weblogic.rmi.cluster.ClusterableServerRef.invoke(ClusterableServerRef.java:230)
    at weblogic.rmi.internal.BasicServerRef$1.run(BasicServerRef.java:520)
    at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:363)
    at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:146)
    at weblogic.rmi.internal.BasicServerRef.handleRequest(BasicServerRef.java:516)
    at weblogic.rmi.internal.wls.WLSExecuteRequest.run(WLSExecuteRequest.java:118)
    at weblogic.work.ExecuteThread.execute(ExecuteThread.java:256)
    at weblogic.work.ExecuteThread.run(ExecuteThread.java:221)

I have wlclient.jar, wlsafclient.jar, and weblogic.jar in my buildpath. I have tried all sorts of combinations of adding/removing these jars, but I still get the same error regardless of what I do. Any help would be greatly appreciated.

4
  • 1
    remove all jars and just use weblogic.jar ensure that you are taking jar from <weblogic_installation_directory>\server\lib directory Commented May 31, 2012 at 17:46
  • I have already tried that. These jars are from my server installation. If I only use the weblogic.jar, I get the following exception:Exception in thread "main" java.lang.NoClassDefFoundError: weblogic/security/subject/AbstractSubject Commented May 31, 2012 at 17:49
  • I have to try with weblogic 12c, I just ran the same code with Weblogic 10 and it worked correctly just with weblogic.jar Commented May 31, 2012 at 18:12
  • 1
    According to this forum post: kr.forums.oracle.com/forums/thread.jspa?threadID=940386, you need to build and use this jar: wlfullclient.jar. This explains how to build it: docs.oracle.com/cd/E12839_01/web.1111/e13717/jarbuilder.htm Commented May 31, 2012 at 18:46

4 Answers 4

6

After doing some research, I am deleting my old answer and starting over.

There is a large table of client types in the Oracle Doc for WebLogic Standalone Clients. For each type of client, listed, the table shows the required jar files. For certain types of clients, you need to build an additional jar (wlfullclient.jar) and include that.

Hope this helps.

Sign up to request clarification or add additional context in comments.

6 Comments

Are you doing this in a JSP? I have to set the property values for the initial context in mine since it is just a client java application.
Yes, there was a comment from mprabhat here a minute ago that explained that my code would only work when run within the Web App. So that explains why this won't work for you.
Completely rewrote my answer after researching the issue a bit.
BINGO! Works like a charm now. Thanks for the help!
I created the wlfullclient.jar, and I removed all other jars. I recompiled, and it worked. There must have been a missing class that was included when I built the wlfullclient.jar.
|
0

I have also face this problem and I tried to add "wlfullclient.jar" to my directory to fix it out but I didn't find this jar file in weblogic installation folder.

But at the last I have set all required jar files form weblogic by using setDomainEnv.cmd and it works fine. Here we don't have to care about which jar files required or not it'll simply set classpath for all required jar file for your program.

I am using Weblogic 11g.

1 Comment

0

In Weblogic 12c, copy the weblogic.jar file to some other directory. Rename the file to weblogic-classes.jar and then build the jar file using wljarbuilder.

Add the newly created wlfullclient.jar file to your Class Path in eclipse.

Comments

0

Build wlfullclient.jar and add just this jar to the build path. It solved the problem for me. By the way weblogic.jar from Weblogic 12 is missing some classes as compared to weblogic.jar from Weblogic 10.3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.