簡體   English   中英

如何使用給定的JNDI名稱連接到Websphere數據源?

[英]How do I connect to a Websphere Datasource with a given JNDI name?

我正在使用Websphere Portal 7.0並使用RAD 8.0創建一個portlet。 我的portlet正在嘗試與遠程服務器建立db2連接。 我在本地編寫了一個java程序來與服務器建立基本的JDBC連接,並從表中獲取記錄。 代碼工作正常; 但是,當我將代碼添加到我的portlet以及db2jcc4.jar時,連接不起作用。 我正在使用基本的:

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

我認為使用Websphere數據源是正確的方法。 我知道數據源的JNDI名稱,但我沒有找到關於如何建立連接的明確示例。 有幾個例子使用了一個DataSource類(我輸入了這個類,這看起來好像它來自一個原生的java包,所以我在這里使用什么導入?)加上一個Context。 我遇到過如下代碼:

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

我的整個web.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" />

Websphere Portal似乎很平庸。 我的代碼現在正在工作並連接到數據庫。

您需要在應用程序中定義資源引用 ,然后在部署期間將該邏輯資源引用映射到物理資源(數據源)。

在您的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中創建的數據源。

在您的代碼中,您可以獲得類似於您在示例中顯示它的方式的DataSource; 但是,您將用於查找它的JNDI名稱實際上應該是您定義的資源引用名稱( res-ref-name ),而不是物理數據源的JNDI名稱。 此外,您還需要在res-ref-name前加上應用程序命名上下文( java:comp/env/ )。

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

要從數據源獲取連接,以下代碼應該有效:

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 Data Sources配置中定義的數據源(即通過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,preprod,prod)時,這非常有用。

服務DNS

需要了解JNDI,理解它是服務定位器。 當所需服務托管在與應用程序相同的服務器/節點上時,您可以使用InitialContext。

更復雜的是,在Web Sphere中定義數據源(至少在4.0之后)允許您定義不同程度的可見性。 基本上,它將命名空間添加到環境中,客戶端必須知道資源的托管位置。

簡單的例子

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

這是IBM的參考頁面

如果您嘗試從不在J2EE容器中的應用程序引用數據源,則需要稍微不同的方法,從類路徑中需要一些J2EE客戶端jar開始。 http://www.coderanch.com/t/75386/Websphere/lookup-datasources-JNDI-outside-EE

查找以下代碼以從您的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文件或編輯項目中的任何內容。
而已..

對於像我這樣的人,只需要有關如何使用JNDI查找從Java連接到(DB2)WAS數據源的信息(使用的IBM Websphere 8.5.5和DB2 Universal JDBC Driver Provider與實現類:com.ibm.db2.jcc。 DB2ConnectionPoolDataSource):

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

傑森,

這是它的工作原理。

Localnamespace - java:comp / env是應用程序使用的本地名稱空間。 您在其中使用的名稱jdbc / db只是一個別名。 它不是指物理資源。

在部署期間,此別名應映射到在WAS / WPS運行時定義的物理資源(在您的情況下是數據源)。

這實際上存儲在ejb-bnd.xmi文件中。 在最新版本中,XMI替換為XML文件。 這些文件稱為綁定文件。

HTH Manglu

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM