0

Here's my persistence xml file

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
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">
<persistence-unit name="GoodreadsJpa">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>entity.Book</class>
    <class>entity.Review</class>
    <class>entity.UserActionLog</class>
    <class>entity.User</class>
    <properties>
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/goodreads_clone?useUnicode=true&amp;useJDBCCompliantTimezoneShift=true&amp;useLegacyDatetimeCode=false&amp;serverTimezone=UTC;" />
        <property name="eclipselink.jdbc" value="jdbc:mysql://localhost:3306/goodreads_clone?useUnicode=true&amp;useJDBCCompliantTimezoneShift=true&amp;useLegacyDatetimeCode=false&amp;serverTimezone=UTC;"/>
        <property name="javax.persistence.jdbc.user" value="root" />
        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
    </properties>
</persistence-unit>

As you can see I have provided jdbc url for connecting to the database. However, when I run my application I get the following information.

14:31:57,377 INFO  [org.eclipse.persistence.connection] (default task-2) Connected: jdbc:h2:mem:test
User: ROOT
Database: H2  Version: 1.3.173 (2013-07-28)
Driver: H2 JDBC Driver  Version: 1.3.173 (2013-07-28)

which states that I connected to jdbc:h2:mem:test and consequently I cannot perform the desired actions.

It makes me think I am connected to a wrong database?Am I missing something? How can I actually connect to the db that I want?

I am using Wildfly 10 and EclipseLink. Not using Maven.

Marios Ath
  • 618
  • 2
  • 9
  • 21
  • 1
    Application-managed or container-managed em? If the latter, you've probably defined a data source in the server config and that's the one that is getting picked up – crizzis Apr 01 '19 at 12:45
  • Container managed. I see. I have to modify the standalone.xml in my wildfly server then? – Marios Ath Apr 01 '19 at 12:56
  • 1
    That would be my guess (most probably that's where the `jdbc:h2:mem:test` is coming from). You should then refer to your datasource using the `persistence-unit.jta-data-source` (or `persistence-unit.non-jta-data-source`) tag in your persistence unit definition. If you need both the MySQL and H2 data sources, you can create multiple persistence units and differentiate between them using `@PersistenceContext(name = "...")` – crizzis Apr 01 '19 at 13:09
  • @crizzis Yes that worked. Thank you for the solution, if you want you can add an answer and I'll accept it as the correct one. – Marios Ath Apr 01 '19 at 13:58

2 Answers2

1

1.You need to add mysql driver to Jboss like here : Can't add mysql driver to jboss

or here https://synaptiklabs.com/posts/adding-the-mysql-jdbc-driver-into-wildfly/

  1. You need add mysql datasource in standalone.xml configuration file like here : https://zorq.net/b/2011/07/12/adding-a-mysql-datasource-to-jboss-as-7/

    <datasource jndi-name="java:/mydb" pool-name="my_pool" enabled="true" jta="true" use-java-context="true" use-ccm="true"> <connection-url>jdbc:mysql://localhost:3306/mydb</connection-url> <driver>mysql</driver> <security> <user-name>root</user-name> <password>root</password> </security> <statement> <prepared-statement-cache-size>100</prepared-statement-cache-size> <share-prepared-statements /> </statement> </datasource>

1

Assuming you're using container-managed em, you should define your data source in Wildfly configuration (standalone.xml). You should then refer to your datasource using the persistence-unit.jta-data-source (or persistence-unit.non-jta-data-source) tag in your persistence unit definition.

If you need both the MySQL and H2 data sources, you can create multiple persistence units and differentiate between them using @PersistenceContext(name = "...")

crizzis
  • 9,978
  • 2
  • 28
  • 47