将 GlassFish/Payara 连接到 MySQL 8.x

Connecting GlassFish/Payara to MySQL 8.x

我有一个 Maven JSF/JPA Web 应用程序连接到 MySQL 5.x 使用 Netbeans 12 开发。在我更新 MySQL 版本从 5.x 到 8.x。自该更新以来,我无法配置数据库以连接到 JSF 应用程序。与 MySQL 8.x 的连接在 Netbeans 中有效,但在部署应用程序时无效。

当前配置包括 EclipseLink 2.7.7、MySQL8.0.23 和 GlassFish 5(5.0.1) / Payara 5(5.2021.1)。无法成功连接到 MySQL。我也未能在 GlassFish 和 Payara 管理控制台的 JDBS 连接池中建立连接。谁能告诉我 MySQL 版本 8 链接到 Payara 或 GlassFish 的来源?

Payara管理控制台中显示的错误如下。

An error has occurred Ping Connection Pool failed for pooConnection.
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. Please check the server.log for more details. 

日志文件包含以下内容。

[javax.enterprise.resource.resourceadapter.com.sun.enterprise.connectors.service] [tid: _ThreadID=161 _ThreadName=admin-thread-pool::admin-listener(3)] [timeMillis: 1613549343463] [levelValue: 900] [[
  RAR8054: Exception while creating an unpooled [test] connection for pool [ pooConnection ], 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.]]

[2021-02-17T13:39:03.472+0530] [Payara 5.2021.1] [SEVERE] [] [org.glassfish.admingui] [tid: _ThreadID=139 _ThreadName=admin-thread-pool::admin-listener(1)] [timeMillis: 1613549343472] [levelValue: 1000] [[
  RestResponse.getResponse() gives FAILURE.  endpoint = 'http://localhost:4848/management/domain/resources/ping-connection-pool.json'; attrs = '{id=pooConnection}']]

为了连接到 Payara Server,我发现唯一有效的方法是直接在域管理控制台中创建连接池。

现在看来可能很远,但完成后就会成为第二天性:

下载 MySQL8 Java 连接器可用 https://dev.mysql.com/downloads/connector/j/ 并解压到任意文件夹:

它会解压成类似这样的东西: mysql-连接器-java-8.0.23 2/mysql-连接器-java-8.0.23.jar

1) 确保您已启动 Payara 服务器并且 运行:

cd PATH_TO_PAYARA/bin

2)Start/Restart它

./asadmin start-domain

注意:这将默认启动 domain1

3) 安装 MySQL8 连接器

./asadmin add-library PATH_TO_MYSQL_CONNECTOR.jar

4)(必需)重启 Payara

./asadmin restart-domain

5) 通过 http://localhost:4848/common/index.jsf

访问管理控制台

6) 在边栏中导航至“JDBC”->“JDBC 连接池”菜单

7) 从那里单击“新建”以添加新的连接池

想吃多少就吃多少。所以如果你以前尝试过,你可以把你的游泳池放在那里。

8) 在:新建 JDBC 连接池(第 1 步,共 2 步)

Pool Name: MySQL8Pool (or whatever you want)
Resource Type: javax.sql.DataSource
Database Driver Vendor: MySQL8

单击“下一步

9) 向下滚动到“附加属性”

Select 全部和“删除属性”

10) 添加 6 个属性 keys/values 为:

DatabaseName  YOUR_DB_NAME
User YOUR_DB_USER
Password YOUR_DB_PASSWORD
ServerName localhost
PortNumber 3306
UseSSL false

单击“保存

11) 在边栏中导航至“JDBC”->“JDBC 资源”菜单

12) 从那里单击“新建”以添加新的 JDBC 资源

并填写:

JNDI Name: jdbc/MySQL8App
PoolName: MySQL8Pool

单击“保存

从现在开始我假设你正在使用 Maven。

13) 在你 pom.xml 中确保你有 Eclipse Persistence 在你的标签中:

  <dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>org.eclipse.persistence.core</artifactId>
    <version>2.7.7</version>
  </dependency>
  <dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>org.eclipse.persistence.asm</artifactId>
    <version>2.7.7</version>
  </dependency>
  <dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>org.eclipse.persistence.antlr</artifactId>
    <version>2.7.7</version>
  </dependency>
  <dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>org.eclipse.persistence.jpa</artifactId>
    <version>2.7.7</version>
  </dependency>
  <dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>org.eclipse.persistence.jpa.jpql</artifactId>
    <version>2.7.7</version>
  </dependency>
  <dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>org.eclipse.persistence.moxy</artifactId>
    <version>2.7.7</version>
  </dependency>
  <dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>javax.persistence</artifactId>
    <version>2.2.1</version>
  </dependency> 

14) 在

中创建持久化单元

在 persistence.xml 文件中:

<persistence-unit name="mysql8PU" transaction-type="JTA">
  <jta-data-source>jdbc/MySQL8App</jta-data-source>
  <exclude-unlisted-classes>false</exclude-unlisted-classes>
  <shared-cache-mode>NONE</shared-cache-mode>
  <!--properties>
    <property name="javax.persistence.schema-generation.database.action" value="drop-and-create OR create OR complete remove this line"/-->
  </properties>
</persistence-unit>

请注意,在 jta-data-source 中,它指向 jdbc/MySQL8App 并且从现在开始,它可以在任何地方使用在您的代码中的哪个位置,以便在构建 Payara 之后我们现在可以注入它。

PersistenceService.java

package com.your.package.services

import javax.enterprise.context.ApplicationScoped;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@ApplicationScoped
public class PersistenceService {

  @PersistenceContext(unitName = "mysql8PU")
  EntityManager entityManager;
}

现在重建并重新运行您的项目,一切都应该没问题!

只需四步即可配置您的连接:

  1. 复制 Mysql JDBC 驱动程序 JAR 到 $PAYARA_HOME/glassfish/domains/$YOUR_DOMAIN/lib/ e. g.

    cp mysql-连接器-java-8.0.22.jar /opt/payara5/glassfish/domains/domain1/lib/

  2. 创建 XML 资源描述符文件,将其命名为 glassfish-resources.xml。指定适当的参数:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<resources>
    <jdbc-connection-pool connection-creation-retry-interval-in-seconds="30" connection-validation-method="auto-commit" datasource-classname="com.mysql.cj.jdbc.MysqlDataSource" wrap-jdbc-objects="false" res-type="javax.sql.DataSource" name="mysql_mydb_rootPool" is-connection-validation-required="true" connection-creation-retry-attempts="10" validate-atmost-once-period-in-seconds="60">
        <property name="User" value="root"/>
        <property name="Password" value="secret"/>
        <property name="URL" value="jdbc:mysql://localhost:3306/voyager?zeroDateTimeBehavior=convertToNull&serverTimezone=UTC&useSSL=false"/>
        <property name="driverClass" value="com.mysql.cj.jdbc.Driver"/>
        <property name="characterEncoding" value="utf-8"/>
    </jdbc-connection-pool>
    <jdbc-resource enabled="true" jndi-name="jdbc/mydb" object-type="user" pool-name="mysql_mydb_rootPool"/>
</resources>
  1. 向 Payara 添加资源

    $PAYARA_HOME/bin/asadmin 添加资源 glassfish-resources.xml

  2. 重新启动您的域

    $PAYARA_HOME/bin/asadmin 重启域