0

I'm using apache-tomee-webprofile-9.0.0-M7 to try to get into JSF. To that end, I decided to adapt the Weld JSF "login" example. The first thing that I decided to do is to connect persistence to MySql. Here is the datasource being seen by the container:

02-Jul-2022 18:42:08.123 INFO [main] org.apache.openejb.config.AutoConfig.processResourceRef Auto-linking resource-ref 'openejb/Resource/jdbc/MySqlDS' in bean weld-login.Comp387377349 to Resource(id=weld-login/jdbc/MySqlDS)

But here is what gets used - attempting to use HSQL (at least I assume that's what this means):

02-Jul-2022 18:42:55.464 INFO [http-nio-8080-exec-5] null.openjpa.Runtime Starting OpenJPA 3.1.2
02-Jul-2022 18:42:55.568 INFO [http-nio-8080-exec-5] null.openjpa.jdbc.JDBC Using dictionary class "org.apache.openjpa.jdbc.sql.HSQLDictionary" (HSQL Database Engine 2.3.2 ,HSQL Database Engine Driver 2.3.2).
02-Jul-2022 18:42:55.590 INFO [http-nio-8080-exec-5] null.openjpa.jdbc.JDBC Connected to HSQL Database Engine version 2.3 using JDBC driver HSQL Database Engine Driver version 2.3.2.

This is the configuration as far as I've done it. What am I missing? ($CATALINA_HOME/conf/context.xml)

<?xml version="1.0" encoding="UTF-8"?>
<Context>

    <!-- Default set of monitored resources. If one of these changes, the    -->
    <!-- web application will be reloaded.                                   -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>

  <Resource name="jdbc/MySqlDS" auth="Container" type="javax.sql.DataSource"
               maxTotal="100" maxIdle="30" maxWaitMillis="10000"
               username="goose" password="xxx" driverClassName="com.mysql.cj.jdbc.Driver"
               url="jdbc:mysql://nucsrv.home:3306/weldcdi"/>

</Context>

(/META-INF/.persistence.xml)

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd" 
    version="2.1">
<persistence-unit name="loginDatabase">
    <!--
    <jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
    -->
    <jta-data-source>jdbc/MySqlDS</jta-data-source>
    <properties>
        <!--
            <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
            <property name="hibernate.show_sql" value="false"/>
        -->
            <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://nucsrv.home/weldcdi"/> 
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL8Dialect" />
            <property name="javax.persistence.jdbc.user" value="goose"/> 
            <property name="javax.persistence.jdbc.password" value="xxx"/>
            <property name="hibernate.show_sql" value = "true" /> 
            <property name="hibernate.format_sql" value = "true" />
            <property name="javax.persistence.schema-generation.database.action" value="create"/> 
        </properties>
    </persistence-unit>
</persistence>
g00se
  • 3,207
  • 2
  • 5
  • 9
  • Sanity check: Is your persistence file really named `.persistence.xml`? If so, should it not be `persistence.xml` instead? – andrewJames Jul 02 '22 at 18:34
  • Sorry, not it isn't ;) I think I copy/pasted from a swap file in there I wonder if I should name the entity class in there or whether that's not necessary? Also I wonder whether there should be really *any* mention of HIbernate in there since it's not being used? – g00se Jul 02 '22 at 18:49
  • Another thing I wonder about: is it *really* necessary to duplicate the *container's* `Resource` (shown above) in persistence.xml fully? After all, the jndi name of the datasource is already in persistence.xml – g00se Jul 02 '22 at 18:57
  • I have only ever seen a `persistence.xml` file which _does_ define one or more classes explicitly (`org.me.myapp.MyEntity`) - but I don't know what happens if you do not have such an entry (if that is what you are referring to). – andrewJames Jul 02 '22 at 18:57
  • Well I'm going to put the class name in and see if it makes a difference – g00se Jul 02 '22 at 18:58
  • Have you checked this? https://stackoverflow.com/questions/13771783/how-to-define-mysql-data-source-in-tomee – Davide D'Alto Jul 02 '22 at 22:39
  • No, it's not necessary to replicate the properties that are already in the container datasource – Davide D'Alto Jul 02 '22 at 22:40

1 Answers1

0

It seems like you've set the attribute name and not id in the Resource. Should be:

<Resource id="jdbc/MySqlDS" type="javax.sql.DataSource">
...
</Resource>

Example taken from the Apache TomEE guide.

Davide D'Alto
  • 7,421
  • 2
  • 16
  • 30
  • Thanks. That could have been it. I've actually moved on now and configured a new, much more JPA-like datasource in tomee.xml and simply named the `jta-data-source` in persistence.xml. I would have preferred though to configure everything wholly in persistence.xml and wonder if that's possible? – g00se Jul 03 '22 at 07:27
  • I think so. But in that case the container won't manage the datasource anymore so it won't be available somewhere else if you need it. – Davide D'Alto Jul 03 '22 at 09:19
  • I take the point but the trouble is that you really want one backing db per web app. Since the mysql datasource config confines you to *one* db, it's not really hugely useful to have a permanent datasource for mysql configured in the container. Or am I missing something? – g00se Jul 03 '22 at 13:04
  • Really? I haven't done that in a while but it should be possible to configure multiple datasources. You can also have multiple persistence units. So I wouldn't expect that to be an issue. – Davide D'Alto Jul 03 '22 at 13:09
  • No, what I'm getting at is that what you want (in order for it to be useful *in the container*) is to have a pool for the whole db server. Otherwise if there has to be one for each web app, it might surely as well be left in the web app itself and not the container? – g00se Jul 03 '22 at 13:14
  • You can use a resources.xml to configure a DS on a per application basis and use the JNDI name in the persistence.xml only. – rzo1 Jul 06 '22 at 06:23