Search code examples
jpajakarta-eeh2websphere-libertyopen-liberty

How to use H2 database with JPA on WebSphere Liberty


I have a very simple web application running on WebSphere Application Server 18.0.0.2. The app is packaged into WAR and put under dropins (for simplicity).

My server.xml looks like this:

<?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>

I have a very simple entity and a service (stateless EJB):

@Stateless
public class CustomerService {

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

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

And my persistence.xml under META-INF looks like this:

<?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>

I thought these simple configs should be enough to be able to deploy and run this hello-world-type of app. But I am getting an error at runtime:

[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 injection fails. From IBM's docs it's not clear what else should be done.

I have no other XML files (config files) in my app.

Am I missing something ?


Solution

  • The main issue I see is the <datasource> definition in server.xml, you have used the <properties.db2.jcc> element, which corresponds to the IBM DB2 JCC JDBC driver. Since Liberty does not have a dedicated <properties.h2> configuration, you must use the generic <properties> config element, as well as defining the DataSource class names on your <jdbcDriver> element.

    The config should look something like this:

    <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>
    

    Also, it would be better if you put your H2 JDBC driver somewhere under ${server.config.dir} or ${shared.resource.dir}, since ${wlp.install.dir}/lib is where jars of the Liberty runtime are. You don't want to mix your application jars in with those!

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

    Lastly, ensure that your persistence.xml file is in the correct location in your WAR application. It should be at WEB-INF/classes/META-INF/persistence.xml


    As an intermediate debugging step, you can check to make sure the DataSource is resolvable from your application like this:

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

    Once you get that part working, move onto injecting your EntityManager. Also, be sure to check ${server.config.dir}/logs/messages.log for more detailed error messages if things are going wrong.