1

I have a Web Application in Netbeans 11 (with JDK 8 installed), I have added the dependencies for mysql-connector-java-8.0.15.jar and I have generated the JPAs from my DB.

No error while building it.

But when I try to run it, the Glassfish server give me these errors (pastebin link).

This is my persistence.xml, that I set accordingly, is:

<?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="InvoicesPU" transaction-type="JTA">
    <jta-data-source>java:app/Invoices</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties/>
  </persistence-unit>
</persistence>

And in my java class I call it by:

@Stateless
public class InvoiceEJB implements InvoiceEJBLocal {

    @PersistenceContext(unitName = "InvoicesPU")
    EntityManager em;
    ...
    em.someMethod();

For completeness, I have no error during the compilation phase!

Anyone know how to solve it?

Community
  • 1
  • 1

3 Answers3

3

Try to put your JDBC driver jar to Glassfish domain's lib folder. I usually store them in lib/ext.

devmind
  • 344
  • 2
  • 10
  • I checked, in the Glassfish domain there is already the mysql-connector driver! –  Apr 19 '19 at 17:57
  • I fount this [similar problem](https://stackoverflow.com/questions/53378880/glassfish-5-and-mysql-connector) and I checked and all the things about glassfish and jdbc in the Services window are now ok, but I don't know how to add the `Datasource Classname: com.mysql.cj.jdbc.MysqlConnectionPoolDataSource` part. Where can I add it??? –  Apr 19 '19 at 18:20
  • Does Ping work for your connection pool in GF admin console? – devmind Apr 19 '19 at 20:14
  • Probably I don't have the correct Connection Pool in Glassfish, I have only [these](https://imgur.com/a/saziMMH) –  Apr 19 '19 at 22:11
  • 1
    Create connection pool and JDBC resource. You can refer to [this tutorial](https://javatutorial.net/configure-glassfish-mysql). Resource JNDI name goes to `jta-data-source` tag: `jdbc/tutorialsDS` – devmind Apr 19 '19 at 22:44
  • Ok, I created the Connection Pool and the Resource now! And set the additional properties and the `jta-data-source` tag. But in the Admin Console of Glassfish, in the Connection Pool newly created if I test it with Ping... it says me [that](https://imgur.com/a/0bXVptB) Have I forgot something? –  Apr 20 '19 at 08:52
  • Solved! I removed as dependency the `mysql-connector-java-8.x.x.jar` and I addet the `mysql-connector-java-5.x.x.jar` and everything goes fine. I don't know how to solve with the newest connector! :_( –  Apr 20 '19 at 09:08
  • 1
    Class names were changed in version 8. Change data source classname from `com.mysql.jdbc.jdbc2.optional.MysqlDataSource` to `com.mysql.cj.jdbc.MysqlDataSource`. Check [connector docs](https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-api-changes.html) for details. – devmind Apr 21 '19 at 19:20
3

After some research I solved it in this way:

  1. taking inspiration from a similar problem, I created the Connection Pool and the Resource in the Glassfish Admin Console as suggested by devmind following this.

  2. I removed as dependency the mysql-connector-java-8.x.x.jar and I added the mysql-connector-java-5.x.x.jar

  3. In the persistence.xml I set the JNDI name for the tag jta-data-source

And everything goes as expected!

I don't know how to make everything work with the latest version of the MySQL Connector but, at least, in this way my Web Application works.

EDIT: for latests versions of mysql-connector, as devmind has suggest you should set MysqlDataSource: from com.mysql.jdbc.jdbc2.optional.MysqlDataSource to com.mysql.cj.jdbc.MysqlDataSource as reported here.

I hope it's useful to someone else.

0

I do not remember if I got this info from some other post or from the Glassfish user manual but it solves the issue mentioned for me, i.e.: Class name is wrong or classpath is not set ERROR with Glassfish 5 or Payara server. I'm using Payara Server 5.2022.3 currently.

Add the latest connector j to your server/your_domain; in my case it is domain1 and this is the script after I cd to /bin: C:\Program Files\payara-web-5.2022.3\payara5\bin>asadmin add-library --type app "C:\Program Files (x86)\MySQL\Connector J 8.0\mysql-connector-j-8.0.31.jar"

Where quoted string is the path where I keep my connector j.

M.K
  • 1,464
  • 2
  • 24
  • 46