简体   繁体   中英

Application using Pooled JDBC Connections

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 (ie 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)

我认为可以通过执行bea或weblogic \\ user_projects \\ domains \\ base_domain \\ bin> setDomainEnv.cmd解决此问题

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 Java EE 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.

也许是类路径问题,甚至是jvm版本的东西?

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 !

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.

I was having similar problem but fixed after creating wlfullclient.jar and keeping it in class-path.

https://docs.oracle.com/cd/E12840_01/wls/docs103/client/jarbuilder.html#wp1078098

Server : weblogic 10.3.6

DS JNDI : jdbc/hr

DataSource dataSource = null;
    Context ctx = null;
    Properties p = new Properties();

    p.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
    p.put(Context.PROVIDER_URL, "t3://localhost:7001");
    ctx = new InitialContext(p);
    if (ctx != null) {
        dataSource = (DataSource) ctx.lookup("jdbc/hr");
    }

    if (dataSource != null) {
        Connection connection = dataSource.getConnection();
        ResultSet rs = connection.createStatement().executeQuery(
                "Select sysdate from dual");
        while (rs.next()) {
            System.out.println(rs.getString(1));
        }
        connection.close();
    }

Even I'm able to invoke it from stand alone spring application...

<bean id="applicationServerEnviromentProperties"
    class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="properties">
        <props>
            <prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop>
            <prop key="java.naming.provider.url">t3://localhost:7001</prop>
        </props>
    </property>
</bean>

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName">
        <value>jdbc/hr</value>
    </property>
    <property name="jndiEnvironment">
        <ref local="applicationServerEnviromentProperties" />
    </property>
</bean>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM