有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用给定的JNDI名称连接到Websphere数据源?

我正在使用WebSpherePortal7.0并使用RAD8.0创建一个portlet。我的portlet正在尝试与远程服务器建立db2连接。我在本地编写了一个java程序,用于与服务器进行基本的JDBC连接,并从表中获取记录。代码运行良好;但是,当我将代码添加到portlet以及db2jcc4时。jar,连接不工作。我使用的是基本的:

Connection connection = DriverManager.getConnection("jdbc:db2://server:port/db:user=user;password=pw;");

我认为使用Websphere数据源是正确的方法。我知道数据源的JNDI名称,但我没有找到关于如何建立连接的明确示例。有几个示例使用了一个数据源类(我输入了这个类,但它似乎不是来自本机java包,所以我在这里使用什么导入?)加上上下文。我遇到过这样的代码:

Context ctx = new InitialContext();
ctx.lookup("jdbc/xxxx");

。。。 有人能帮我把这个分解一下吗

编辑1

我已根据列出的答案更新了代码。我真的觉得我越来越近了。下面是我的getConnection()方法:

private Connection getConnection() throws SQLException {
    javax.naming.InitialContext ctx = null;
    javax.sql.DataSource ds = null;
    System.out.println("Attempting connection..." + DateUtil.now() );
    try {
        ctx = new javax.naming.InitialContext();
        ds = (javax.sql.DataSource) ctx.lookup("java:comp/env/jdbc/db");
        connection = ds.getConnection();
    } catch (NamingException e) {
        System.out.println("peformanceappraisalstatus: COULDN'T CREATE CONNECTION!");
        e.printStackTrace();
    }       
    System.out.println("connection: " + connection.getClass().getName() + " at " + DateUtil.now());
    return connection;
}

我的整个网络。xml文件看起来像:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name>PeformanceAppraisalStatus</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    <resource-ref>
        <description>
        Datasource connection to Db</description>
        <res-ref-name>jdbc/db</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>
</web-app>

我看到一个错误,它描述了您告诉我Websphere应该提示我做的事情,但没有:

SRVE0169I: Loading Web Module: PeformanceAppraisalStatus.
[8/23/11 18:08:02:166 CDT] 00000009 InjectionProc E   CWNEN0044E: A resource reference binding could not be found for the jdbc/db resource reference, defined for the PeformanceAppraisalStatus component.
[8/23/11 18:08:02:169 CDT] 00000009 InjectionEngi E   CWNEN0011E:  The injection engine failed to process bindings for the metadata.

是的,我知道我在整个应用程序中将性能误认为是性能

解决方案

我是如此的接近。以下是使这一切得以实现的缺失部分:

web.xml:
<resource-ref>      
    <description>
    Datasource connection to db</description>
    <res-ref-name>jdbc/db</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
    <mapped-name>jdbc/db</mapped-name>      
</resource-ref>

ibm-web-bnd.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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/mydatasource" />
</web-bnd>

似乎ibm web bnd。xml文件处理websphere中项目资源名称和数据源之间的绑定。一旦我添加了这一行:

<resource-ref name="jdbc/db" binding-name="jdbc/mydatasource" />

WebSpherePortal似乎得到了安抚。我的代码正在工作并正在连接到数据库


共 (6) 个答案

  1. # 1 楼答案

    查找以下代码以从web应用服务器获取数据库连接。只需在app server中创建数据源,并使用以下代码获得连接:

    // To Get DataSource
    Context ctx = new InitialContext();
    DataSource ds = (DataSource)ctx.lookup("jdbc/abcd");
    // Get Connection and Statement
    Connection c = ds.getConnection();
    stmt = c.createStatement();
    

    导入命名和sql类。无需添加任何xml文件或编辑项目中的任何内容
    就这样

  2. # 2 楼答案

    对于像我这样的人来说,使用JNDI查找(使用IBM Websphere 8.5.5和DB2 Universal JDBC驱动程序提供程序,实现类为:com.IBM.DB2.jcc.DB2ConnectionPoolDataSource)的Java数据源只需要关于如何连接到(DB2)的信息:

    public DataSource getJndiDataSource() throws NamingException {
        DataSource datasource = null;
        InitialContext context = new InitialContext();
        // Tomcat/Possibly others: java:comp/env/jdbc/myDatasourceJndiName
        datasource = (DataSource) context.lookup("jdbc/myDatasourceJndiName");
        return datasource;
    }
    
  3. # 3 楼答案

    要从数据源获取连接,应使用以下代码:

    import java.sql.Connection;
    
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.sql.DataSource;
    
    Context ctx = new InitialContext();
    DataSource dataSource = ctx.lookup("java:comp/env/jdbc/xxxx");
    Connection conn = dataSource.getConnection();
    
    // use the connection
    
    conn.close();
    

    虽然您可以直接查找Websphere数据源配置中定义的数据源(即通过Websphere控制台),但从java:comp/env/jdbc/xxxx进行的查找意味着web中需要有一个条目。xml:

    <resource-ref>
        <res-ref-name>jdbc/xxxx</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>
    

    这意味着数据源可以映射到每个应用程序,如果要将应用程序指向其他数据源,则无需更改数据源的名称。这在将应用程序部署到需要指向不同数据库的不同服务器(例如test、Prepod、prod)时非常有用

  4. # 4 楼答案

    服务的DNS

    需要了解JNDI是一个服务定位器。当所需服务与应用程序托管在同一服务器/节点上时,您可以使用InitialContext

    更复杂的是,在WebSphere中定义数据源(至少在4.0中)允许您定义不同程度的可见性。基本上,它将名称空间添加到环境中,客户端必须知道资源的托管位置

    Simple example

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

    这是IBM的reference page

    如果您试图从不在J2EE容器中的应用程序引用数据源,那么您需要一种稍微不同的方法,首先在类路径中需要一些J2EE客户机JARhttp://www.coderanch.com/t/75386/Websphere/lookup-datasources-JNDI-outside-EE

  5. # 5 楼答案

    您需要在应用程序中定义资源引用,然后在部署期间将该逻辑资源引用映射到物理资源(数据源)

    web.xml中,添加以下配置(根据需要修改名称和属性):

    <resource-ref>
        <description>Resource reference to my database</description>
        <res-ref-name>jdbc/MyDB</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>
    

    然后,在应用程序部署期间,WAS将提示您将此资源引用(jdbc/MyDB)映射到您在WAS中创建的数据源

    在您的代码中,您可以获得与示例中显示的数据源类似的数据源;但是,用于查找的JNDI名称实际上应该是您定义的资源引用的名称(res-ref-name),而不是物理数据源的JNDI名称。此外,还需要在res-ref名称前面加上应用程序命名上下文(java:comp/env/

    Context ctx = new InitialContext();
    DataSource dataSource = (DataSource) ctx.lookup("java:comp/env/jdbc/MyDB");
    
  6. # 6 楼答案

    杰森

    这就是它的工作原理

    Localnamespace-java:comp/env是应用程序使用的本地名称空间。在jdbc/db中使用的名称只是一个别名。它不是指物理资源

    在部署期间,应将此别名映射到WAS/WPS运行时定义的物理资源(在您的情况下是数据源)

    这实际上存储在EJBBND中。xmi文件。在最新版本中,XMI被XML文件取代。这些文件称为绑定文件

    嗯 芒鲁