java 在 WebSphere 8.5 中查找 JDBC 数据源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17449022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Lookup a JDBC DataSource in WebSphere 8.5
提问by user2546624
I want to use a JDBC Connetion in my webapp which is configured in WebSphere. (Like this one here: How to use JDBC in JavaEE?)
我想在 WebSphere 中配置的 web 应用程序中使用 JDBC 连接。(就像这里的这个:如何在 JavaEE 中使用 JDBC?)
I had used this DataSource before via JPA but our customer wants to have native SQL ... don't ask.
我之前通过 JPA 使用过这个数据源,但我们的客户想要原生 SQL ......不要问。
I found a lot of examples and tutorial (e.g. http://www.wickcentral.com/java/dl/ds_resreferencesetts_Websphere.pdf, Websphere JNDI lookup fails) but nothing want work.
我发现了很多示例和教程(例如http://www.wickcentral.com/java/dl/ds_resreferencesetts_Websphere.pdf,Websphere JNDI 查找失败)但没有任何工作要做。
The DataSource in the WebSphere has the JNDI-Name "jdbc/myDS"
WebSphere 中的数据源具有 JNDI 名称“jdbc/myDS”
I added a resource-ref to my web.xml:
我在 web.xml 中添加了一个资源引用:
<resource-ref>
<res-ref-name>jdbc/myDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
And I tryed to get the DataSource in my Dao:
我尝试在我的 Dao 中获取数据源:
ds = (DataSource) new InitialContext()
.lookup("java:comp/env/jdbc/myDS");
But what I get is a
但我得到的是一个
com.ibm.wsspi.injectionengine.InjectionException: CWNEN0044E: A resource reference binding could not be found for the following resource references [jdbc/myDS], defined for the MyAPP component.
I tryed a lot. Did anyone sees the fault?
我尝试了很多。有没有人看到错误?
回答by Cristian Meneses
Did you match your web-app defined datasource with a Websphere defined datasource during installation? Websphere usually asks you to bind resources when they are detected on the installation process (If I remember correctly, it is in the step named "Map reference resources to resources").
在安装过程中,您是否将 Web 应用定义的数据源与 Websphere 定义的数据源相匹配?Websphere 通常会在安装过程中检测到资源时要求您绑定资源(如果我没记错的话,它在名为“将引用资源映射到资源”的步骤中)。
Other common issue is a Websphere datasource in a different context (Cell/Node/Server) than your app, so it cannot be found at runtime.
另一个常见问题是 Websphere 数据源位于与您的应用程序不同的上下文(单元/节点/服务器)中,因此无法在运行时找到它。
回答by maher
You need to add the binding in ibm-web-bnd.xml:
您需要在 ibm-web-bnd.xml 中添加绑定:
<resource-ref name="jdbc/myDS" binding-name="jdbc/myDS" />