java 使用池化 JDBC 连接的应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/774018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:42:40  来源:igfitidea点击:

Application using Pooled JDBC Connections

javajdbcweblogicjndi

提问by Nate

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.)

我正在使用一个旧的 WebLogic 应用程序,其中包含一个 Web 服务应用程序和一个独立的命令行应用程序。两者都需要访问公共数据库,我想尝试让命令行应用程序使用到 Web 服务器的 JDBC 连接的池连接。(独立应用程序只能在服务器处于活动状态时运行,并且两者都将在同一台物理机器上运行。)

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

我一直在尝试使用 JNDI 查找 JDBC 驱动程序,如下所示:

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.

我已经确认 JNDI 名称是正确的。我能够使用其他 Web 应用程序连接到数据库,但我的独立应用程序仍然有困难。- 我从WebLogic 应用笔记中得到了这个想法。

Any ideas on what I have overlooked?

关于我忽略了什么的任何想法?

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

编辑 1.1:我看到一个“java.lang.ClassCastException: java.lang.Object”异常。

EDIT 2:When I perform the following:

编辑 2:当我执行以下操作时:

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:

我试图在 web 应用程序中测试相同的代码块(在原始问题中描述)并且能够连接到数据源(即它似乎“工作”)。这个工作测试报告:

"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.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.access0(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)

回答by Oren Yosifon

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

客户端 JVM 中 Oracle 驱动程序的 JAR 可能与服务器中的 JAR 不同。确保相同的 Oracle JDBC 驱动程序 jar 位于命令行工具和 weblogic webapp 的类路径中

good luck !

祝你好运 !

回答by Gary

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.

这个异常看起来像是在服务器端生成的。我要做的第一件事是验证 WebLogic Server 的版本并检查客户端应用程序的类路径。您需要确保您的客户端应用程序具有与 WebLogic Server 相同版本的 WebLogic 客户端 jar 文件。您在客户端的类路径中包含了 weblogic 客户端 jar 文件,对吗?由于您使用的是 WebLogic 的 RMI 驱动程序,因此我认为您不需要客户端类路径中的任何 Oracle jar 文件。您的客户端本质上是在与 WebLogic Server 交流 RMI。您配置的 WebLogic Server 连接池知道如何与 Oracle 对话。在连接池中使用什么 JDBC 驱动程序并不重要。

回答by sathish

I think this problem is resolved by executing bea or weblogic\user_projects\domains\base_domain\bin>setDomainEnv.cmd

我认为这个问题是通过执行 bea 或 weblogic\user_projects\domains\base_domain\bin>setDomainEnv.cmd 解决的

回答by Robin

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.

您是否将应用程序设置为瘦客户端?当您不在其中一个 Java EE 容器中操作时,进行查找的上下文和方法的工作方式不同(因供应商而异)。您可能想研究如何从这样的客户端使用 Weblogic 查找资源,以及如何正确设置它。

回答by KarlP

Perhaps a classpath issue or even a jvm version thing?

也许是类路径问题甚至是 jvm 版本问题?

回答by sourcerebels

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

一种可能性:您导入了不正确的“DataSource”类:-)

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

一个想法,希望对你有帮助

回答by Ahmed

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

我遇到了类似的问题,但在创建 wlfullclient.jar 并将其保留在类路径中后修复。

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

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

Server : weblogic 10.3.6

服务器:weblogic 10.3.6

DS JNDI : jdbc/hr

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

即使我能够从独立的 spring 应用程序中调用它......

<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>