我正在尝试在我的本地 [Websphere Liberty][3] 服务器上运行在 [IBM Bluemix][2] 中创建的 [Java DB Web Starter][1],但它一直出现以下错误
[错误] CWWJP0029E:服务器在 liberty-IRDS.war 模块和 JavaDBApp 应用程序中找不到 openjpa-todo 持久性单元。 [错误] CWNEN0035E:无法解析 JavaDBApp 应用程序的 liberty-IRDS.war 模块中空组件的 javax.persistence.EntityManager 类型的 java:comp/env/openjpa-todo/entitymanager 引用。 [err] javax.naming.NamingException: CWNEN1001E: 无法实例化 java:comp/env/openjpa-todo/entitymanager JNDI 名称引用的对象。如果引用名称映射到执行 JNDI 查找的应用程序的部署描述符绑定(bind)中的 JNDI 名称,请确保部署描述符绑定(bind)中的 JNDI 名称映射是正确的。如果 JNDI 名称映射正确,请确保可以使用相对于默认初始上下文的指定名称解析目标资源。 [根异常是 com.ibm.wsspi.injectionengine.InjectionException:JavaDBApp 应用程序的 liberty-IRDS.war 模块中空组件的 javax.persistence.EntityManager 类型的 java:comp/env/openjpa-todo/entitymanager 引用无法解决。] [错误] 在 com.ibm.ws.injectionengine.osgi.internal.naming.InjectionJavaColonHelper.newCannotInstantiateObjectException (InjectionJavaColonHelper.java:183) [错误]在[内部类(class)] [错误] 在 javax.naming.InitialContext.lookup(未知来源) [错误] 在 example.jpa.TODOListResource.getEm(TODOListResource.java:184) [错误] 在 example.jpa.TODOListResource.(TODOListResource.java:35)
完成的步骤:
- 在 Bluemix 中创建 Java DB Web Starter
- 下载代码
按照建议修改 server.xml - 尝试连接到本地数据库和作为 Web Starter 应用程序的一部分托管的数据库 -
--已部署的应用
Server.xml
<!-- Enable features --> <featureManager> <feature>webProfile-7.0</feature> <feature>localConnector-1.0</feature> </featureManager> <!-- To access this server from a remote client add a host attribute to the following element, e.g. host="*" --> <httpEndpoint httpPort="9080" httpsPort="9443" id="defaultHttpEndpoint"/> <!-- Automatically expand WAR files and EAR files --> <applicationManager autoExpand="true"/> <applicationMonitor updateTrigger="mbean"/>
<library id="MySQLLib"> <fileset dir="${server.config.dir}/lib" id="mysql-connector-jar" includes="mysql-connector-java-*.jar"/> </library> <webApplication id="JavaDBApp" location="liberty-IRDS.war" name="JavaDBApp"/> </server>
还尝试过:在 Java DB Web starter 中连接到 DB
<server description="new server"> <!-- Enable features --> <featureManager> <feature>webProfile-7.0</feature> <feature>localConnector-1.0</feature> </featureManager> <!-- To access this server from a remote client add a host attribute to the following element, e.g. host="*" --> <httpEndpoint httpPort="9080" httpsPort="9443" id="defaultHttpEndpoint"/> <!-- Automatically expand WAR files and EAR files --> <applicationManager autoExpand="true"/> <applicationMonitor updateTrigger="mbean"/> <dataSource id="mydbdatasource" jndiName="jdbc/mydbdatasource"> <jdbcDriver libraryRef="MySQLLib"/> <properties url="db2://user13711:2gGhHNZvJwKc@75.126.155.153:50000/SQLDB" user="user13711" password="****"/> </dataSource> <library id="MySQLLib"> <fileset dir="${server.config.dir}/lib" id="mysql-connector-jar" includes="mysql-connector-java-*.jar"/> </library> <webApplication id="JavaDBApp" location="liberty-IRDS.war" name="JavaDBApp"/> </server>
最佳答案
如果您开始使用样板应用程序,则该应用程序在 bluemix 上已正确配置开箱即用。它通过打开 cloudAutoWiring-1.0
来完成此操作server.xm 中的功能。这将自动将 jdbc/mydbdatasource 连接到应用程序的 web.xml 中的资源引用。
在本地使用样板:
您需要采取一些步骤将应用程序连接到服务器,因为 bluemix 无法在本地环境中为您执行此操作。
- 确保在 server.xml 中启用了 jdbc 功能(因为
webProfile-7.0
打开了jdbc-4.1
) - 在 server.xml 中使用 jndi 名称 `"jdbc/mydbdatasource"在 server.xml 中配置数据源
server.xml 中的数据源配置如下所示:
<dataSource id='mydbdatasource' jndiName='jdbc/mydbdatasource' jdbcDriverRef='myJDBCDriver'>
<properties ... /> <!-- depends on what DB you are using -->
</dataSource>
<jdbcDriver id="myJDBCDriver">
<library>
<fileset dir="C:/path/to/your/jdbc/driver.jar"/>
</library>
</jdbcDriver>
如代码示例中所述,<properties>
您使用取决于您使用的 JDBC 驱动程序。
有关用于所有主要 JDBC 驱动程序的属性列表,请查看 IBM's DataSource configuration doc .
关于Bluemix 中的 Java DB Web 启动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34949609/