代码之家  ›  专栏  ›  技术社区  ›  Nate

使用池JDBC连接的应用程序

  •  0
  • Nate  · 技术社区  · 16 年前

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

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

    我已经确认JNDI名称是正确的。我能够用其他web应用程序连接到数据库,但我的独立应用程序仍然有困难。-我是从一个朋友那里得到这个主意的 WebLogic app note

    对我忽略的东西有什么想法吗?

    编辑1.1:

    编辑2:

    Object dsObj = ctx.lookup("dbOracle");
    System.out.println("Obj was: " + dsObj.getClass().getName());
    

    在独立应用程序中,它报告:

    "Obj was: weblogic.jdbc.common.internal._RemoteDataSource_Stub"
    

    "Obj was: weblogic.jdbc.common.internal.RmiDataSource"
    

    此外,这里还有一个失败时的堆栈跟踪:

    ####<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)
    
    7 回复  |  直到 16 年前
        1
  •  1
  •   KarlP    16 年前

    可能是类路径问题,甚至是jvm版本问题?

        2
  •  1
  •   Oren Yosifon    16 年前

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

        3
  •  1
  •   Gary    16 年前

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

        4
  •  1
  •   sathish    13 年前

    我认为通过执行bea或weblogic\user\u projects\domains\base\u domain\bin>setDomainEnv.cmd

        5
  •  1
  •   Arjan Tijms Mike Van    12 年前

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

        6
  •  0
  •   sourcerebels    16 年前

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

    DataSource ds = (DataSource) ctx.lookup ("dbOracle");
    

    为此:

    javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup ("dbOracle");
    

    只是一个想法,希望这能对你有所帮助

        7
  •  0
  •   Ahmed    10 年前

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

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

    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();
        }
    

    甚至我也可以从独立的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>