【问题标题】:DB2 data source for a liberty java applicationLiberty Java 应用程序的 DB2 数据源
【发布时间】:2016-12-03 03:36:46
【问题描述】:


我正在尝试通过部署在 liberty websphere 机器上的 java web 应用程序访问远程机器上的 DB2 数据源。在代码执行从上下文查找数据源的那一刻,以下错误返回给我:

I FFDC1015I: An FFDC Incident has been created: "java.lang.ClassNotFoundException com.ibm.db2.jcc.DB2ConnectionPoolDataSource com.ibm.ws.injectionengine.processor.ResourceProcessor.loadTypeClass 1142" at ffdc_16.07.28_15.54.28.0.log"
W CWNEN0046W: The com.ibm.db2.jcc.DB2ConnectionPoolDataSource type specified on the resource-ref, resource-env-ref, or message-destination-ref with the jdbc/db2/primadb name in the db2-webapp.war module could not be loaded. Compatibility type checking will not be performed for this resource reference.

1)对访问数据源有帮助吗?
2)打开我打开了数据源,我想打开一个连接并在数据库上执行一个准备好的stateemnt。这里的代码应该和使用传统的javax.sql.Datasource不同吧?

我的 server.xml 的相关部分

 <dataSource jndiName="jdbc/db2/primadb" type="javax.sql.DataSource">
    <jdbcDriver javax.sql.DataSource="com.ibm.db2.jcc.DB2ConnectionPoolDataSource"
                libraryRef="db2Lib"/>
 <properties.db2.jcc driverType="4" serverName="172.17.0.3"
               portNumber="50000" databaseName="PRIMADB"
               user="db2inst1" password="*****"/>
</dataSource>

<library id="db2Lib">
    <fileset dir="lib" includes="*.jar"/>
</library>
 <application id="db2-webapp" name="Web App DB2">
    <classLoader commonLibraryRef="db2Lib"/>
</application>

【问题讨论】:

  • "java.lang.ClassNotFoundException com.ibm.db2.jcc.DB2ConnectionPoolDataSource" -- 看起来驱动 JAR 不在类路径中。
  • 你能用你的数据源的 server.xml 配置和资源参考更新你的问题吗?另外,您是在应用程序中直接使用 DB2 类(例如 com.ibm.db2.jcc.DB2ConnectionPoolDataSource)还是使用 JDBC 标准接口,例如 javax.sql.DataSource

标签: java jdbc docker db2 websphere-liberty


【解决方案1】:

正如 mustaccio 所提到的,jar 很可能不在类路径中。对于 Liberty,您的 server.xml 中应该有这样的元素:

<library id="DB2JCC4Lib">
    <fileset dir="C:/DB2/java" includes="db2jcc4.jar db2jcc_license_cisuz.jar"/>
</library>

检查以确保 com.ibm.db2.jcc.DB2ConnectionPoolDataSource 存在于您指定的位置(并且您具有读取权限)。

更多信息:Configuring relational database connectivity in Liberty

【讨论】:

  • 我用 server.xml 的相关部分编辑了问题。这是我的库参考,文件具有读取权限。
【解决方案2】:

我终于解决了这个问题。

问题是我的 DB2 jar 在我的文件系统上不可读。

这里是我的配置的相关代码部分以供将来参考: 服务器.xml

<server description="Default server">
...
<dataSource jndiName="jdbc/db2/primadb" type="javax.sql.DataSource">
   <jdbcDriver libraryRef="db2Lib"/>
   <properties.db2.jcc  serverName="172.17.0.3"
                   portNumber="50000" databaseName="PRIMADB"
                   user="db2inst1" password="*****"/>
</dataSource>
<library id="db2Lib">
   <fileset dir="${shared.resource.dir}/db2" includes="*.jar"/>
</library>
...
</server>

我的项目部署描述符 web.xml

<web-app>
   ...
   <resource-ref>
    <res-ref-name>jdbc/db2/primadb</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
   </resource-ref>
   ...
</web-app>

最后是访问数据库的java代码:

Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup("jdbc/db2/primadb");
Connection conn = ds.getConnection();
try {
    Statement stmt = conn.createStatement();
    String first_name = request.getParameter("firstname");
    String last_name = request.getParameter("lastname");
    String country = request.getParameter("country");
    String sql = "INSERT INTO PRIMA_USER (id, firstname, lastname, country) VALUES (DEFAULT, '" + first_name
                        + "', '" + last_name + "' , '" + country + "')";
    stmt.execute(sql);
} finally {
    conn.close();
}

感谢大家帮助解决问题。

【讨论】:

  • webProfile-6.0 更改为7.0 只会将您从jdbc-4.0 更改为4.1 功能。在这两种情况下,DB2 的工作方式都是一样的。唯一的问题是您的库没有读取权限。 server.xml 中的功能更改无关紧要。
  • @aguibert,这是真的,我也尝试回到 webProfile 6.0,它工作得很好。谢谢
猜你喜欢
  • 1970-01-01
  • 2017-12-14
  • 2016-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-15
  • 2013-03-13
相关资源
最近更新 更多