java - 使用 ElephantSQL 在 Bluemix 中部署 Liberty 应用程序时找不到有效的 JDBC 驱动程序

标签 java postgresql jdbc ibm-cloud websphere-liberty

我有一个 WebSphere Liberty 16.0.0.2 Web 应用程序,我在笔记本电脑上本地开发并运行它,并使用 JPA 使用 Derby DB 来处理数据库。我曾经使用 SQLDB(2016 年 5 月由 IBM 停用)将它部署到 Bluemix 中,并且在自动连接服务时一切正常。由于 SQLDB 不再可用,我能在 Bluemix 上找到的唯一免费 SQL 是 ElephantSQL,但是 Autowiring 似乎不起作用。这是我在部署后得到的错误:

Program error occured: CWWJP0013E: The server cannot locate the java:comp/env/jdbc/TriReplicatorDB data source for the TriReplicatorPersistenceUnit persistence unit because it has encountered the following exception: javax.naming.NamingException: CWNEN1001E: The object referenced by the java:comp/env/jdbc/TriReplicatorDB JNDI name could not be instantiated. If the reference name maps to a JNDI name in the deployment descriptor bindings for the application performing the JNDI lookup, make sure that the JNDI name mapping in the deployment descriptor binding is correct. If the JNDI name mapping is correct, make sure the target resource can be resolved with the specified name relative to the default initial context. [Root exception is com.ibm.wsspi.injectionengine.InjectionException: CWNEN0030E: The server was unable to obtain an object instance for the java:comp/env/jdbc/TriReplicatorDB reference. The exception message was: CWNEN1006E: The server was unable to obtain an object for the jdbc/TriReplicatorDB binding with the javax.sql.DataSource type. The exception message was: java.sql.SQLNonTransientException: DSRA4000E: A valid JDBC driver implementation class was not found for the jdbcDriver jdbcDriver[myDerbyJDBCdriver] using the library com.ibm.ws.classloading.sharedlibrary_79. [/home/vcap/app/wlp/usr/servers/triServer/lib/postgresql-jdbc-9.4.1209.jar]]. 

我上传 web 应用程序的方式是打包服务器并使用 cf push 命令。这是生成的 server.xml 文件:

<server description="new server">
<!-- Enable features -->
<featureManager>
    <feature>jsp-2.3</feature>
    <feature>concurrent-1.0</feature>
    <feature>jaxrs-2.0</feature>
    <feature>jaxrsClient-2.0</feature>
    <feature>jpa-2.1</feature>
    <feature>appSecurity-2.0</feature>
    <feature>localConnector-1.0</feature>
    <feature>icap:managementConnector-1.0</feature>
    <feature>appstate-1.0</feature>
    <feature>jdbc-4.1</feature>
    <feature>cloudAutowiring-1.0</feature>
</featureManager>
<httpEndpoint httpPort="${port}" id="defaultHttpEndpoint" host="*"/>
<webContainer deferServletLoad="false" trustHostHeaderPort="true" extractHostHeaderPort="true"/>
<applicationMonitor updateTrigger="mbean"/>
<!-- This is the application itself -->
<webApplication contextRoot="/" id="Tri-Replicator" location="Tri-Replicator.war" name="Tri-Replicator">
</webApplication>
<jdbcDriver id="myDerbyJDBCdriver">
    <library name="DerbyLib">
        <fileset dir="C:\projects_c\Tri-Replicator-16\db\db-derby-10.11.1.1-bin\lib" includes="derby.jar"/>
        <fileset dir='${server.config.dir}/lib' includes='postgresql-jdbc-9.4.1209.jar'/>
    </library>
</jdbcDriver>
<!-- Use local Derby DB for local testing on laptop, but when deployed into BlueMix this will automatically be rewired to use SQL DB instance -->
<dataSource id="DerbyConnection" jdbcDriverRef="myDerbyJDBCdriver" jndiName="jdbc/TriReplicatorDB">
    <properties createDatabase="create" databaseName="${cloud.services.ElephantSQL-tri-replicator.connection.name}" user="${cloud.services.ElephantSQL-tri-replicator.connection.user}" password="${cloud.services.ElephantSQL-tri-replicator.connection.password}" serverName="${cloud.services.ElephantSQL-tri-replicator.connection.host}" portNumber="${cloud.services.ElephantSQL-tri-replicator.connection.port}"/>
</dataSource>
<include location='runtime-vars.xml'/>
<httpDispatcher enableWelcomePage='false'/>
<config updateTrigger='mbean'/>
<appstate appName='Tri-Replicator' markerPath='${home}/../.liberty.state'/>

作为替代问题 - 有人可以为 Bluemix 中的 SQL 数据库推荐其他小型免费计划吗? PostgreSQL 和 DB2 不提供免费计划。

最佳答案

要使用 PostgreSQL(这是 ElephantSQL 使用的 JDBC 驱动程序),您需要在 <jdbcDriver> 上指定数据源实现类名。元素:

<jdbcDriver id="myDerbyJDBCdriver" javax.sql.DataSource="org.postgresql.ds.PGPoolingDataSource">
  <!-- keep nested stuff the same -->
</jdbcDriver>

您需要为 PostgreSQL 而不是为 Derby 指定 DataSource impl 类的原因是因为 Derby 是 Liberty 而不是 PostgreSQL 的“已知”数据库。

我会让 Liberty 开发人员知道这一点,以便将来某个时候可以将 PostgreSQL 添加为“已知”数据库。

关于java - 使用 ElephantSQL 在 Bluemix 中部署 Liberty 应用程序时找不到有效的 JDBC 驱动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39370482/

相关文章:

java - 在 RxJava 中调用 Observable 的任何订阅者回调之前执行操作

java - 在 HTML 广告中实现 MRaid.js

ruby-on-rails - PG::ConnectionBad Error when trying to run rake db:migrate 在创建模型后,使用 postgresql

postgresql - PostgreSQL 中的 SELECT 查询

java - JBoss EAP6 - 无法创建 JDBC 数据源 (Microsoft SQL Server)

java - Spring REST Web 服务 appContext 失败

java - QuickBooks Online Java SDK DataServiceexecuteBatch() 导致 NullPointerException

postgresql - 在 CentOS 8,7 上检查 timetz 时出现 Postgresql 13.0 编译问题

mysql - 我可以使用 mvn install :install-file? 安装 mysql jdbc 连接器吗

java - Oracle 驱动程序的内部结构