如何使用给定的JNDI名称连接到Websphere数据源? [英] How do I connect to a Websphere Datasource with a given JNDI name?
问题描述
连接连接= DriverManager.getConnection(jdbc:db2:// server:port / db :user = user; password = pw;);
我认为使用Websphere数据源是正确的方法。我知道数据源的JNDI名称,但是我没有找到关于如何建立连接的明确的例子。几个示例使用DataSource类(我键入这个,这似乎不是来自一个本机java包,所以我在这里使用什么导入))加上一个上下文。我遇到过如下代码:
上下文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(尝试连接...+ 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:COULD NOT CREATE CONNECTION!);
e.printStackTrace();
}
System.out.println(connection:+ connection.getClass()。getName()+at+ DateUtil.now());
返回连接;
}
我的整个web.xml文件如下所示:
<?xml version =1.0encoding =UTF-8?>
< web-app id =WebApp_IDversion =2.5xmlns =http://java.sun.com/xml/ns/javaeexmlns:xsi =http://www.w3 .org / 2001 / XMLSchema-instancexsi: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>
数据源连接到Db< / description>
< res-ref-name> jdbc / db< / res-ref-name>
< res-type> javax.sql.DataSource< / res-type>
< res-auth>容器< / res-auth>
< res-sharing-scope>可共享< / res-sharing-scope>
< / resource-ref>
< / web-app>
我看到一个错误,描述了你们告诉我的东西Websphere应该提示我做,但不是:
SRVE0169I:加载Web模块:PeformanceAppraisalStatus。
[8/23/11 18:08:02:166 CDT] 00000009 InjectionProc E CWNEN0044E:为PeformanceAppraisalStatus组件定义的jdbc / db资源引用找不到资源引用绑定。
[8/23/11 18:08:02:169 CDT] 00000009 InjectionEngine E CWNEN0011E:注入引擎未能处理元数据的绑定。
是的,我知道我在整个应用程序中将性能拼写为性能。
解决方案
我非常亲密以下是丢失的位,使其全部落到了位置:
web.xml:
< resource-ref> ;
< description>
数据源连接到db< / description>
< res-ref-name> jdbc / db< / res-ref-name>
< res-type> javax.sql.DataSource< / res-type>
< res-auth>容器< / res-auth>
< res-sharing-scope>可共享< / res-sharing-scope>
< mapped-name> jdbc / db< / mapped-name>
< / resource-ref>
ibm-web-bnd.xml:
<?xml version =1.0encoding =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 / dbbinding-name =jdbc / mydatasource/>
< / web-bnd>
似乎ibm-web-bnd.xml文件处理项目资源名称与websphere中的数据源。一旦我添加了一行:
< resource-ref name =jdbc / dbbinding-name =jdbc / mydatasource />
Websphere Portal似乎安抚了。我的代码正在工作并连接到数据库。
您需要在应用程序中定义资源引用,然后将该逻辑资源引用映射到在部署期间的物理资源(数据源)。
在您的 web.xml
中,添加以下配置(修改适当的名称和属性):
< resource-ref>
< description>对我的数据库的资源引用< / description>
< res-ref-name> jdbc / MyDB< / res-ref-name>
< res-type> javax.sql.DataSource< / res-type>
< res-auth>容器< / res-auth>
< res-sharing-scope>可共享< / res-sharing-scope>
< / resource-ref>
然后,在应用程序部署期间,WAS将提示您映射此资源引用( jdbc / MyDB
)到您在WAS中创建的数据源。
在代码中,您可以获得类似于 ve在你的例子中显示出来;然而,您将用于查找的JNDI名称实际上应该是您定义的资源引用的名称( res-ref-name
),而不是JNDI名称物理数据源。此外,您需要在应用程序命名上下文( java:comp / env /
)前缀res-ref-name。
上下文ctx = new InitialContext();
DataSource dataSource =(DataSource)ctx.lookup(java:comp / env / jdbc / MyDB);
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.
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):
<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>
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/
).
Context ctx = new InitialContext();
DataSource dataSource = (DataSource) ctx.lookup("java:comp/env/jdbc/MyDB");
这篇关于如何使用给定的JNDI名称连接到Websphere数据源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!