如何连接到具有给定 JNDI 名称的 Websphere 数据源?
我正在使用 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 似乎很满意。我的代码现在正在运行并连接到数据库。
I'm using Websphere Portal 7.0 and creating a portlet with RAD 8.0. My portlet is trying to make a db2 connection to a remote server. I wrote a java program locally to do a basic JDBC connection to the server and get records from a table. The code works fine; however, when I add the code to my portlet as well as the db2jcc4.jar, the connection doesn't work. I'm using the basic:
Connection connection = DriverManager.getConnection("jdbc:db2://server:port/db:user=user;password=pw;");
I figure that using the Websphere datasource is the right way to go. I know the JNDI name for the datasource, but I'm not finding clear cut examples on how to make a connection. Several examples use a DataSource class (I typed this in and this doesn't seem like it comes from a native java package so what import do I use here?) coupled with a Context. I've come across code like:
Context ctx = new InitialContext();
ctx.lookup("jdbc/xxxx");
...
Can someone break this down for me?
EDIT 1
I've updated my code per the answers listed. I really think I'm getting closer. Here is my getConnection() method:
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;
}
My entire web.xml file looks like:
<?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>
I am seeing an error that describes the very thing you guys are telling me Websphere should prompt me to do, but doesn't:
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.
Yes, I know that I've mispelled performance as peformance throughout the app.
SOLUTION
I was so very close. Here are the missing bits that made it all fall into place:
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>
It appears that the ibm-web-bnd.xml file handles the binding between the project resource name and the datasource in websphere. Once I added the line:
<resource-ref name="jdbc/db" binding-name="jdbc/mydatasource" />
Websphere Portal seemed appeased. My code is working and connecting to the database now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您需要在应用程序中定义资源引用,然后在部署期间将该逻辑资源引用映射到物理资源(数据源)。
在
web.xml
中,添加以下配置(根据需要修改名称和属性):然后,在应用程序部署期间,WAS 将提示您映射此资源引用 (
jdbc/MyDB< /code>) 添加到您在 WAS 中创建的数据源。
在您的代码中,您可以获取类似于示例中显示的数据源;但是,用于查找的 JNDI 名称实际上应该是您定义的资源引用的名称 (
res-ref-name
),而不是物理数据源的 JNDI 名称。此外,您还需要在 res-ref-name 前面添加应用程序命名上下文 (java:comp/env/
)。You need to define a resource reference in your application and then map that logical resource reference to the physical resource (data source) during deployment.
In your
web.xml
, add the following configuration (modifying the names and properties as appropriate):Then, during application deployment, WAS will prompt you to map this resource reference (
jdbc/MyDB
) to the data source you created in WAS.In your code, you can obtain the DataSource similar to how you've shown it in your example; however, the JNDI name you'll use to look it up should actually be the resource reference's name you defined (
res-ref-name
), rather than the JNDI name of the physical data source. Also, you'll need to prefix the res-ref-name with the application naming context (java:comp/env/
).要从数据源获取连接,应使用以下代码:
虽然您可以直接查找 Websphere 数据源配置中定义的数据源(即通过 websphere 控制台),但从 java:comp/env/jdbc 查找/xxxx 意味着 web.xml 中需要有一个条目:
这意味着数据源可以映射到每个应用程序的基础上,如果您想将应用程序指向,则无需更改数据源的名称不同的数据源。当将应用程序部署到需要指向不同数据库的不同服务器(例如测试、预生产、生产)时,这非常有用。
To get a connection from a data source, the following code should work:
While you can look up a data source as defined in the Websphere Data Sources config (i.e. through the websphere console) directly, the lookup from java:comp/env/jdbc/xxxx means that there needs to be an entry in web.xml:
This means that data sources can be mapped on a per application bases and you don't need to change the name of the data source if you want to point your app to a different data source. This is useful when deploying the application to different servers (e.g. test, preprod, prod) which need to point to different databases.
处理服务
JNDI 的 DNS 需要理解它是一个服务定位器。当所需的服务与应用程序托管在同一服务器/节点上时,您可以使用 InitialContext。
让它变得更加复杂的是,在Web Sphere 中定义数据源(至少在4.0 中)允许您定义不同程度的可见性。基本上,它将命名空间添加到环境中,并且客户端必须知道资源的托管位置。
简单示例。
这是 IBM 的 参考页面。
如果您尝试从不在 J2EE 容器中的应用程序引用数据源,那么您需要采用稍微不同的方法,首先需要在类路径中添加一些 J2EE 客户端 jar。 http://www.coderanch.com/t/ 75386/Websphere/lookup-datasources-JNDI-outside-EE
DNS for Services
JNDI needs to be approached with the understanding that it is a service locator. When the desired service is hosted on the same server/node as the application, then your use of InitialContext may work.
What makes it more complicated is that defining a Data Source in Web Sphere (at least back in 4.0) allowed you to define the visibility to various degrees. Basically it adds namespaces to the environment and clients have to know where the resource is hosted.
Simple example.
Here is IBM's reference page.
If you are trying to reference a data source from an app that is NOT in the J2EE container, you'll need a slightly different approach starting with needing some J2EE client jars in your classpath. http://www.coderanch.com/t/75386/Websphere/lookup-datasources-JNDI-outside-EE
杰森,
这就是它的工作原理。
本地命名空间 - java:comp/env 是应用程序使用的本地命名空间。您在其中使用的名称 jdbc/db 只是一个别名。它不指物理资源。
在部署期间,此别名应映射到在 WAS/WPS 运行时定义的物理资源(在您的情况下为数据源)。
这实际上存储在 ejb-bnd.xmi 文件中。在最新版本中,XMI 已替换为 XML 文件。这些文件称为绑定文件。
华泰
曼鲁
Jason,
This is how it works.
Localnamespace - java:comp/env is a local name space used by the application. The name that you use in it jdbc/db is just an alias. It does not refer to a physical resource.
During deployment this alias should be mapped to a physical resource (in your case a data source) that is defined on the WAS/WPS run time.
This is actually stored in ejb-bnd.xmi files. In the latest versions the XMIs are replaced with XML files. These files are referred to as the Binding files.
HTH
Manglu
对于像我这样的人,只需要有关如何使用 JNDI 查找从 Java 连接到 (DB2) WAS 数据源的信息(使用 IBM Websphere 8.5.5 和 DB2 Universal JDBC Driver Provider 以及实现类:com.ibm.db2.jcc .DB2ConnectionPoolDataSource):
For those like me, only needing information on how to connect to a (DB2) WAS Data Source from Java using JNDI lookup (Used IBM Websphere 8.5.5 & DB2 Universal JDBC Driver Provider with implementation class: com.ibm.db2.jcc.DB2ConnectionPoolDataSource):
查找以下代码以从您的 Web 应用程序服务器获取数据库连接。只需在应用程序服务器中创建数据源并使用以下代码来获取连接:
导入命名和 sql 类。无需添加任何 xml 文件或编辑项目中的任何内容。
就是这样..
Find below code to get database connection from your web app server. Just create datasource in app server and use following code to get connection :
Import naming and sql classes. No need to add any xml file or to edit anything in project.
That's it..