0

I searched for similar topics. There are plenty of situations with this kind of problem but none was exactly mine, and none provided help. I am completly stuck at the moment, so I would really appreciate your help. Here's the situation:

I am trying to build a very simple app and, for now, just trying to connect everything together. My config files are the following: pom.xml (containing a profile to facilitate the deployment):

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.example</groupId>
    <artifactId>hello</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>8.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.23</version>
        </dependency>

        <dependency>
            <groupId>fish.payara.extras</groupId>
            <artifactId>payara-embedded-all</artifactId>
            <version>5.182</version>
            <scope>test</scope>
        </dependency>

    </dependencies>
    <build>
        <finalName>hello</finalName>
    </build>

    <profiles>
        <profile>
            <id>payara</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>fish.payara.maven.plugins</groupId>
                        <artifactId>payara-micro-maven-plugin</artifactId>
                        <version>1.0.1</version>
                        <executions>
                            <execution>
                                <phase>package</phase>
                                <goals>
                                    <goal>bundle</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>
                            <useUberJar>true</useUberJar>
                            <deployWar>true</deployWar>
                            <payaraVersion>5.182</payaraVersion>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <failOnMissingWebXml>false</failOnMissingWebXml>
    </properties>
</project>

my web.xml (under webapp/WEB-INF):

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <data-source>
        <name>java:global/hello</name>
        <class-name>com.mysql.jdbc.Driver</class-name>
        <url>jdbc:mysql://localhost:3306/connectiondatabase</url>
        <user>root</user>
        <password>mypass</password>
    </data-source>
</web-app>

This database is running ok, and it was perfectly accessed from a general db client (dbeaver).

And finally, my persistence.xml (under resources/META-INF):

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

<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             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="hello" transaction-type="JTA">
        <jta-data-source>java:global/hello</jta-data-source>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
        </properties>
    </persistence-unit>

</persistence>

I have a simple ping resource and JAXRS config class that came as default with the archtype I used to create the project (airhacks) and just one more simple entity, as follows:

package com.airhacks.entities;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

@Entity
@Table(name = "players")
@NamedQuery(name = Player.FIND_PLAYER_BY_NAME, query = "select player from Player player where player.name =:" + Player.PLAYER_PARAMETER)
public class Player {
  public static final String PLAYER_PARAMETER = "Player.name";
  public static final String FIND_PLAYER_BY_NAME = "Player.findByName";

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;

  private String name;

  private Integer age;

  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Integer getAge() {
    return age;
  }

  public void setAge(Integer age) {
    this.age = age;
  }
}

At this point, I can deploy the app with no errors (mvn clean package payara-micro:start), although when checking the database, no table is created (I thought persistence-unit would take care of that with both 'exclude-unlisted-classes' and 'drop-and-create', but apparently, something else is needed).

After that, for testing purposes, I am adding a queryService class, so I can actually use my datasource. I am adding the following class:

package com.airhacks.services;

import com.airhacks.entities.Player;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.PersistenceContext;

@Stateless
public class QueryService {

  @PersistenceContext(unitName = "hello")
  EntityManager entityManager;

  public Player getPlayerByName(String name) {
    try {
      return entityManager.createNamedQuery(Player.FIND_PLAYER_BY_NAME, Player.class)
        .setParameter(Player.PLAYER_PARAMETER, name)
        .getSingleResult();
    } catch (NoResultException | NonUniqueResultException e) {
      return null;
    }
  }
}

As far as I understand, there's no need to detail anything else on the PersistenceContext annotation, as we only have one persistence unit (although I also added unitName="hello", with the same result). But once I try to deploy the app, I get the following errors:

[2021-02-13T13:50:07.023+0000] [] [SEVERE] [NCLS-CORE-00026] [javax.enterprise.system.core] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1613224207023] [levelValue: 1000] [[ Exception during lifecycle processing javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.7.1.qualifier): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: java.sql.SQLException: Error in allocating a connection. Cause: The driver could not be loaded: com.mysql.jdbc.Driver Error Code: 0 ...

  • 2.7.1.qualifier): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: java.sql.SQLException: Error in allocating a connection. Cause: The driver could not be loaded: com.mysql.jdbc.Driver Error Code: 0 at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:316) ... ... 41 more Caused by: java.sql.SQLException: Error in allocating a connection. Cause: The driver could not be loaded: com.mysql.jdbc.Driver ... com.sun.appserv.connectors.internal.api.PoolingException:

So, basically, stating that the driver could not be loaded. And this is where I can't do much. My external libs contain indeed mysql, so I don't understand where this problems comes from. I already tried with different db (SQLite, for example), but I get the very same problem, with different drivers, of course. Would really appreciate your help and sorry for the long post. Thank you!

external libs

Gonçalo
  • 13
  • 4

1 Answers1

0

This version of Mysql uses another driver class, change you web.xml from:

<class-name>com.mysql.jdbc.Driver</class-name>

to:

<class-name>com.mysql.cj.jdbc.MysqlConnectionPoolDataSource</class-name>

Reference: https://docs.payara.fish/community/docs/5.2020.5/documentation/payara-server/jdbc/mysql-connectorj-8-changes.html

Delfino Gomes
  • 375
  • 2
  • 12
  • After changing the class-name on web.xml to com.mysql.cj.jdbc.MysqlConnectionPoolDataSource, a new exception is thrown: org.eclipse.persistence.exceptions.DatabaseException Internal Exception: java.sql.SQLException: Error in allocating a connection. Cause: Connection could not be allocated because: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. ... And so on. Any help? Thanks – Gonçalo Feb 16 '21 at 15:26
  • Try to connect from a database client, like workbench, check if this database "connectiondatabase" is already created. That's the only things I can think of right now. – Delfino Gomes Feb 17 '21 at 22:29
  • I appreciate the answer. Yes, I've done that, successfully, the db is up and running – Gonçalo Feb 19 '21 at 08:16
  • Well, looks like there are several possible problems that could cause this exception, a thorough explanation can be found here: https://stackoverflow.com/a/10772407/1256973 – Delfino Gomes Feb 19 '21 at 11:47