0
votes

I'm trying to access a datasource connection via Java client with this code:

Connection conn;
Statement stmt;
ResultSet rs;

try {
Properties prop = new Properties();
prop.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
prop.put(javax.naming.Context.PROVIDER_URL, "t3://localhost:7001");
Context ctx = new InitialContext(prop);
Object obj = ctx.lookup("test/datasource");
System.out.println("Data Source Found….");

DataSource ds = (DataSource) obj;
conn = ds.getConnection();
System.out.println("Data Source User Name::"+conn.getMetaData().getUserName());
stmt = conn.createStatement();

String query = "select 1 from dual";
System.out.println("Query " + query);
rs = stmt.executeQuery(query);
if (rs != null) {
    System.out.println("Some Data Found in Query");
} else {
    System.out.println("No Data Found in Query");
}
ctx.close();
} catch (Exception e) {
        e.printStackTrace();
}

But i got the following stack trace. I can't figure out what happening. Someone knows?

Data Source Found…

Feb 13, 2012 4:07:28 PM GMT-03:00 Info JDBC BEA-001155 The following exception has occurred:

java.lang.NullPointerException at weblogic.utils.wrapper.WrapperFactory.getCachedWrapperClass(WrapperFactory.java:55) at weblogic.utils.wrapper.WrapperFactory.getWrapperClass(WrapperFactory.java:202) at weblogic.utils.wrapper.WrapperFactory.getWrapperClass(WrapperFactory.java:190) at weblogic.utils.wrapper.WrapperFactory.createWrapper(WrapperFactory.java:175) at weblogic.jdbc.wrapper.JDBCWrapperFactory.getWrapper(JDBCWrapperFactory.java:197) at weblogic.jdbc.rmi.SerialConnection.readResolve(SerialConnection.java:140) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at java.io.ObjectStreamClass.invokeReadResolve(ObjectStreamClass.java:1061) at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1762) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351) at weblogic.utils.io.ChunkedObjectInputStream.readObject(ChunkedObjectInputStream.java:197) at weblogic.rjvm.MsgAbbrevInputStream.readObject(MsgAbbrevInputStream.java:564) at weblogic.utils.io.ChunkedObjectInputStream.readObject(ChunkedObjectInputStream.java:193) at weblogic.rmi.internal.ObjectIO.readObject(ObjectIO.java:62) at weblogic.rjvm.ResponseImpl.unmarshalReturn(ResponseImpl.java:240) at weblogic.rmi.cluster.ClusterableRemoteRef.invoke(ClusterableRemoteRef.java:348) at weblogic.rmi.cluster.ClusterableRemoteRef.invoke(ClusterableRemoteRef.java:259) at weblogic.jdbc.common.internal.RmiDataSource_1031_WLStub.getConnection(Unknown Source)

Thanks in advance.

1
Have you opened WebLogic console and verified whether everything is OK with your DataSource? Is it testable from the web console? - Viccari
Does this appear in the sysout? System.out.println("Data Source Found…."); - JoseK
Thanks guys. I already solved the problem. I simply changed the connection string on the weblogic console to a format like jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=OCDB)(INSTANCE_NAME=OCDB_1))). It's weird, but worked! - elias

1 Answers

0
votes

If you are using Weblogic 10.3 and above best fool proof solution (That does not make you scratch your head) is:

Include just the wlfullclient.jar found in ${wlserver_10.3_installation_home}\server\lib folder

in you class path (lib). This way you do not need weblogic.jar

If you do not find the wlfullclient.jar then build it. Here is how you build it:

http://docs.oracle.com/cd/E12840_01/wls/docs103/client/jarbuilder.html

Then write this code:

//Some Code

weblogic.jndi.Environment env = new weblogic.jndi.Environment();
env.setInitialContextFactory(Environment.DEFAULT_INITIAL_CONTEXT_FACTORY);
env.setProviderUrl("t3://${weblogic_host_ip}:${weblogic_port_number}");
env.setSecurityPrincipal(${DB_USER});
env.setSecurityCredentials(${DB_PASSWORD});
java.lang.Object obj = null;
try {
            javax.naming.Context ctx = env.getInitialContext();
        obj = ctx.lookup(${JNDI_NAME_OF_DATA_SOURCE});
        javax.sql.DataSource ds = (javax.sql.DataSource) obj;
        java.sql.Connection conn = ds.getConnection();
        java.sql.Statement stmt = conn.createStatement();
        java.sql.ResultSet rs = stmt.executeQuery("select '5' from dual");
        if(rs != null){
        rs.next();
        System.out.println(rs.getString(1));
    }

    } catch (javax.naming.NamingException e) {
        e.printStackTrace();
    }

//Other Code ....