代码之家  ›  专栏  ›  技术社区  ›  ikos23

如何在websphere liberty上使用h2数据库和jpa

  •  2
  • ikos23  · 技术社区  · 6 年前

    我有一个非常简单的web应用程序 WebSphere Application Server 18.0.0.2 是的。这个应用程序被打包成战争并被置于 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.xml 在META-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)
    

    实体管理器注射失败。从ibm的文档来看,还不清楚还应该做些什么。

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

    我遗漏了什么吗?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Andy Guibert    6 年前

    我看到的主要问题是 <datasource> 在server.xml中,您使用了 <properties.db2.jcc> 元素,它对应于ibm db2 jcc jdbc驱动程序。因为自由没有一个 <properties.h2> 配置,必须使用泛型 <properties> 配置元素,以及在 <jdbcDriver> 元素

    配置应该如下所示:

    <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 是自由运行时的罐子。你不想把你的应用程序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 如果出了问题,会有更详细的错误信息。

    推荐文章