如何在WebSphere Liberty上将H2数据库与JPA一起使用

ikos23

我在上运行了一个非常简单的Web应用程序WebSphere Application Server 18.0.0.2该应用程序打包到WAR中,并放在下面dropins(为简单起见)。

我的server.xml样子是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<server description="new server">

    <featureManager>
        <feature>javaee-8.0</feature>
    </featureManager>

    <httpEndpoint id="defaultHttpEndpoint" httpPort="9080" httpsPort="9443" />

    <!-- Automatically expand WAR files and EAR files -->
    <applicationManager autoExpand="true"/>

    <!-- THE JAR IS THERE (UNDER Liberty lib directory) -->
    <library id="H2JDBCLib">
        <fileset dir="${wlp.install.dir}/lib" includes="h2-1.4.197.jar"/>
    </library>

    <!-- AND THIS IS MY DATA SOURCE DEFINITION -->
    <dataSource id="h2test" jndiName="jdbc/h2test">
        <jdbcDriver libraryRef="H2JDBCLib"/>
        <properties.db2.jcc databaseName="testdb" serverName="localhost" portNumber="8082" user="sa" />
    </dataSource>

</server>

我有一个非常简单的实体和一个服务(无状态EJB):

@Stateless
public class CustomerService {

    @PersistenceContext(unitName = "h2test")
    private EntityManager entityManager;

    public List<Customer> getAllCustomers() {
        return entityManager
                .createNamedQuery(FIND_ALL, Customer.class)
                .getResultList();
    }
}

而我的persistence.xmlMETA-INF下看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
   http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
             version="1.0">

    <persistence-unit name="h2test" transaction-type="JTA">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

        <jta-data-source>jdbc/h2test</jta-data-source>

        <exclude-unlisted-classes>false</exclude-unlisted-classes>
    </persistence-unit>

</persistence>

我认为这些简单的配置应该足以部署和运行这种hello-world类型的应用程序。但是我在运行时遇到错误:

[ERROR   ] CNTR0019E:  EJB throws an exception when invoking "getAllCustomers". 

    Details: javax.ejb.EJBException: The java:comp/env/com.my.app.service.CustomerService/entityManager reference of type javax.persistence.EntityManager for the null component in the my-app.war module of the my-app application cannot be resolved.
        at com.ibm.wsspi.injectionengine.InjectionBinding.getInjectionObject(InjectionBinding.java:1489)
        at [internal classes]
        at com.my.app.service.EJSLocalNSLCustomerService_22d8d9f5.getAllCustomers(EJSLocalNSLCustomerService_22d8d9f5.java)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)

EntityManager注入失败。根据IBM的文档,尚不清楚还应该做什么。

我的应用程序中没有其他XML文件(配置文件)。

我想念什么吗?

安迪·吉伯特(Andy Guibert)

我看到的主要问题是<datasource>server.xml中定义,您已经使用了<properties.db2.jcc>元素,该元素对应于IBM DB2 JCC JDBC驱动程序。由于Liberty没有专用的<properties.h2>配置,因此您必须使用通用<properties>config元素,并在<jdbcDriver>元素上定义DataSource类名称

配置应如下所示:

<dataSource id="h2test" jndiName="jdbc/h2test">
    <!-- Define the DataSource class names on the <jdbcDriver> element -->
    <jdbcDriver 
        javax.sql.XADataSource="org.h2.jdbcx.JdbcDataSource"
        javax.sql.ConnectionPoolDataSource="org.h2.jdbcx.JdbcDataSource"
        javax.sql.DataSource="org.h2.jdbcx.JdbcDataSource" 
        libraryRef="H2JDBCLib"/>
    <!-- set the connection URL on the <properties> element.
         this corresponds to the setURL() method on H2's JdbcDataSource class.
         you may also list any properties here that have a corresponding setXXX method on H2's JdbcDataSource class -->
    <properties URL="jdbc:h2:mem:testdb"/>
</dataSource>

另外,最好将H2 JDBC驱动程序放在${server.config.dir}下方${shared.resource.dir},因为这${wlp.install.dir}/lib是Liberty运行时jar所在的位置。您不想将您的应用程序jar与它们混合使用!

<library id="H2JDBCLib">
    <fileset dir="${server.config.dir}" includes="h2-1.4.197.jar"/>
</library>

最后,确保您的persistence.xml文件在WAR应用程序中的正确位置。应该在WEB-INF/classes/META-INF/persistence.xml


作为中间调试步骤,您可以像这样检查以确保可从应用程序中解析出数据源:

@Resource(lookup = "jdbc/h2test")
DataSource ds;

// ...
ds.getConnection().close();

一旦该部分开始工作,请继续注入EntityManager另外,如果出现问题,请确保检查${server.config.dir}/logs/messages.log更详细的错误消息。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将Corda节点扩展为与H2以外的数据库一起使用?

如何在单个服务器上将Hibernate与多个数据库一起使用

如何在Websphere Liberty和Java中使用密钥库?

如何使用H2和JPA创建数据库备份?

如何将Spring Boot与MySQL数据库和JPA一起使用?

与JPA一起使用时,如何让Hibernate在数据库中自动创建表?

如何使用Jhipster再生H2数据库

如何在Liberty WebSphere Server中使用Angular

Elasticsearch如何与Titan数据库一起使用

如何使用 JPA 和 H2 数据库在删除时强制执行外键约束

如何在Spring Security和默认数据库H2中使用Grails数据库迁移?

如何在laravel中使用H2数据库?

如何在使用bpchar的h2数据库中执行单元测试?

如何在飞行中将BLOB数据类型与不同的数据库一起使用?

如何在 WebSphere Liberty 中设置方法级别事务超时?

如何在WebSphere Liberty Batch中配置事务超时?

我想要做的就是使用JPA 2.0访问Websphere V8中的H2 mem数据库

如何在.select语句中将pgcrypto与knex一起使用?(Postgres数据库)

如何在WebSphere上使用基本身份验证?

如何在 C# 上将文本框、下拉列表和 SQL 数据库链接/绑定在一起?

如何在github https存储库地址上将git与ssh一起使用?

我如何使用表格将数据插入我的H2数据库

我如何获得“选择进入”语句以与H2一起使用

如何关闭H2内存数据库?

如何更改H2数据库密码?

如何检查h2数据库的大小?

如何在 H2 数据库中使用包含数组的列来获取 n 1 行

使用GRADLEW BUILD运行时,如何在每个测试类之间删除H2数据库?

将Hibernate与H2数据库和joda DateTime一起使用