【问题标题】:Setting up a datasource with WebSphere Liberty Profile 8.5使用 WebSphere Liberty Profile 8.5 设置数据源
【发布时间】:2013-07-23 22:29:38
【问题描述】:

我的网络应用正在从 JNDI 获取数据源:

javax.naming.InitialContext ctx = new javax.naming.InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource) 
    ctx.lookup("java:comp/env/jdbc/db");

在应用程序的WEB-INF/web.xml 中,我有:

<resource-ref>
    <description>DataSource</description>
    <res-ref-name>jdbc/db</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

在应用程序的WEB-INF/ibm-web-bnd.xml 中,我有:

<web-bnd
    xmlns="http://websphere.ibm.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-web-bnd_1_0.xsd"
    version="1.0">
    <virtual-host name="default_host"/>
    <resource-ref name="jdbc/db" binding-name="jdbc/db"/>
</web-bnd>

在 WebSphere Liberty Profile 的 server.xml 中,我有(保留相关部分):

<server description="new server">

    <featureManager>
        <feature>jsp-2.2</feature>
        <feature>jdbc-4.0</feature>
    </featureManager>

    <library id="oracle-lib">
        <fileset dir="lib" includes="ojdbc5_g.jar"/>
    </library>

    <dataSource jndiName="jdbc/db" jdbcDriverRef="oracle-driver" type="javax.sql.DataSource">
        <jdbcDriver libraryRef="oracle-lib" id="oracle-driver"/>
        <connectionManager numConnectionsPerThreadLocal="10" id="ConnectionManager" minPoolSize="1"/>
        <properties user="user" password="password"
                    url="jdbc:oracle:thin:@//db-server:1521/db"/>
    </dataSource>

</server>

当应用尝试从 JNDI 获取数据源时,它失败并出现以下错误:

CWNEN0030E: The @Resource factory encountered a problem getting
the object instance jdbc/oracle binding object.  The exception message was: 
failed to resolve jdbc/oracle to javax.sql.DataSource: 
javax.naming.NameNotFoundException: 
Intermediate context does not exist: jdbc/oracle

我在这里缺少什么?

【问题讨论】:

    标签: jdbc jndi websphere-liberty


    【解决方案1】:

    WebSphere server.xml

    <server>    
        <featureManager>
            <feature>jndi-1.0</feature>
            <feature>jdbc-4.1</feature>
        </featureManager>
    
        <httpEndpoint id="defaultHttpEndpoint"
                      host="localhost"
                      httpPort="9080"
                      httpsPort="9443" />
    
        <library id="oracle-lib">
            <fileset dir="lib" includes="ojdbc6_g.jar"/>
        </library>
    
        <dataSource jndiName="jdbc/oracle">
            <jdbcDriver libraryRef="oracle-lib"/>
            <properties.oracle user="orbeon" password="password"
                               url="jdbc:oracle:thin:@//localhost:1521/orbeon"/>
        </dataSource>
    
        <application name="orbeon" location="war/orbeon" type="war">
            <classloader commonLibraryRef="oracle-lib"/>
        </application>    
    </server>
    

    WEB-INF/ibm-web-bnd.xml

    <web-bnd version="1.0"
            xmlns="http://websphere.ibm.com/xml/ns/javaee"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-web-bnd_1_0.xsd">
        <virtual-host name="default_host"/>
        <resource-ref name="jdbc/oracle" binding-name="jdbc/oracle"/>
    </web-bnd>
    

    WEB-INF/web.xml

    <resource-ref>
        <res-ref-name>jdbc/oracle</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
    

    资源注入(而不是 web.xml + ibm-web-bnd.xml):

    @Resource(lookup = "jdbc/oracle")
    DataSource ds;
    

    【讨论】:

    • 我有 Websphere Liberty Profile 8.5.5.9,并且不需要 WEB-INF/ibm-web-bnd.xml。此外,对于我的应用来说,仅包含 ojdbc jar 是不够的,它还需要 orai18n jar。 &lt;library id="oracle-lib"&gt;&lt;fileset dir="C:\jars\oracle11g" /&gt;&lt;/library&gt; 工作。
    【解决方案2】:

    我们在 Liberty 8.5.5 上使用 DB2,我们在 server.xml

    <dataSource id="db2" isolationLevel="TRANSACTION_READ_COMMITTED" jndiName="jdbc/db2" type="javax.sql.DataSource">
        <jdbcDriver>
            <library>
                <fileset dir="/usr/lib/java/ibm-db2-universal-driver" includes="db2jcc4.jar, db2jcc_license_cisuz.jar, db2jcc_license_cu.jar"/>
            </library>
        </jdbcDriver>
    
        <properties.db2.jcc databaseName="DB2T" portNumber="21020" serverName="db2t.lvm.de"/>
        <containerAuthData password="{xor}KzspMC04" user="tdvorg"/>
    </dataSource>
    

    也许有帮助。

    罗伯特

    【讨论】:

    • 是的,将&lt;library&gt; 放在&lt;dataSource&gt; 中,就像你为我工作的那样。但是,我最终得到了一个 ClassCastException` 作为驱动程序中的类,由 2 个不同的类加载器加载:应用程序中的一个和驱动程序中的一个。 (我确信在很多情况下这很好,只要您不直接使用驱动程序类。)所以我恢复到顶级库声明并让它工作。我用我使用的设置回答了我自己的问题,但这绝对有帮助!
    • 如果数据库实例不是公共数据库,您可能想隐藏您的用户/密码
    • 我们是否有 GUI 来进行这些更改,就像我们在 WAS 中所做的那样。
    【解决方案3】:

    就我而言,下面的解决方案对我来说很好。

    我在 Eclipse 中创建了一个名为“Resources”的“通用项目”。在这个项目中,我创建了一个文件dataSource.xml,内容如下:

    <server>
        <dataSource id="ccm" jndiName="jdbc/ccm" type="javax.sql.DataSource">
            <jdbcDriver id="oracle-driver" libraryRef="oracle-lib"/>
            <connectionManager id="ConnectionManager" minPoolSize="1" numConnectionsPerThreadLocal="10"/>
            <properties.oracle password="password" url="jdbc:oracle:thin:@128.1.30.150:1521:ccmdes" user="ccm"/>
        </dataSource>
    
       <library id="oracle-lib">
            <fileset dir="C:\Oracle\product\10.1.0\Client_1\jdbc\lib" includes="ojdbc14_g.jar"/>
        </library>
    
        <jdbcDriver id="oracle" libraryRef="oracle-lib"/>
    
    </server>
    

    然后我将这个文件拖放到 Eclipse 中进行服务器配置或在server.xml 中创建行:

    <include location="${shared.config.dir}/dataSource.xml"/>
    

    文件server.xml是这样的:

    <server description="new server">
    
        <!-- Enable features -->
        <featureManager>
            <feature>jsp-2.3</feature>
            <feature>adminCenter-1.0</feature>
            <feature>jdbc-4.1</feature>
            <feature>jndi-1.0</feature>
            <feature>servlet-3.1</feature>
        </featureManager>
    
        <httpEndpoint host="localhost" httpPort="9080" httpsPort="9443" id="defaultHttpEndpoint"/>
    
        <applicationMonitor updateTrigger="mbean"/>                 
    
    <!-- Define your admin user name and password -->
        <quickStartSecurity userName="admin" userPassword="password"/>
    
    <!-- Define a keystore for the HTTPS port -->
        <keyStore id="defaultKeyStore" password="Liberty"/> 
    
    <!-- Allows remote file access for config changes -->
    <remoteFileAccess>
      <writeDir>${server.config.dir}</writeDir>
    </remoteFileAccess> 
    
        <!-- <applicationMonitor updateTrigger="mbean"/> -->
        <webApplication id="TestPage" location="TestPage.war" name="TestPage"/>
        <webApplication id="MonitoriaAtendimento" location="MonitoriaAtendimento.war" name="MonitoriaAtendimento"/>
    
        <include location="${shared.config.dir}/dataSource.xml"/>
    </server>
    

    【讨论】:

      猜你喜欢
      • 2013-07-31
      • 2013-02-03
      • 1970-01-01
      • 2018-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多