当前位置: 首页 > 知识库问答 >
问题:

HikariCP连接错误

邹昊
2023-03-14

我尝试使用HikariCP和mariaDB数据库,但是当我尝试初始化的时候,我得到了下一个错误。

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at com.zaxxer.hikari.pool.HikariPool.initializeConnections(HikariPool.java:581) ~[Dubilets-1.0-SNAPSHOT.jar:?]
at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:152) ~[Dubilets-1.0-SNAPSHOT.jar:?]
at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:73) ~[Dubilets-1.0-SNAPSHOT.jar:?]
at eu.elraro.dubilets.storage.Database.<init>(Database.java:69) [Dubilets-1.0-SNAPSHOT.jar:?]
at eu.elraro.dubilets.storage.MariaDBDatabase.<init>(MariaDBDatabase.java:18) [Dubilets-1.0-SNAPSHOT.jar:?]
at eu.elraro.dubilets.Dubilets.realizaConexion(Dubilets.java:168) [Dubilets-1.0-SNAPSHOT.jar:?]
at eu.elraro.dubilets.Dubilets.inicializaVariables(Dubilets.java:164) [Dubilets-1.0-SNAPSHOT.jar:?]
at eu.elraro.dubilets.Dubilets.onEnable(Dubilets.java:69) [Dubilets-1.0-SNAPSHOT.jar:?]
at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-fdc1440-53fac9f]
at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-fdc1440-53fac9f]
at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-fdc1440-53fac9f]
at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:357) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-fdc1440-53fac9f]
at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:317) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-fdc1440-53fac9f]
at net.minecraft.server.v1_8_R3.MinecraftServer.s(MinecraftServer.java:414) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-fdc1440-53fac9f]
at net.minecraft.server.v1_8_R3.MinecraftServer.k(MinecraftServer.java:378) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-fdc1440-53fac9f]
at net.minecraft.server.v1_8_R3.MinecraftServer.a(MinecraftServer.java:333) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-fdc1440-53fac9f]
at net.minecraft.server.v1_8_R3.DedicatedServer.init(DedicatedServer.java:263) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-fdc1440-53fac9f]
at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:524) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-fdc1440-53fac9f]
at java.lang.Thread.run(Thread.java:745) [?:1.8.0_66]

由以下原因引起:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

我的MariaDBDatabase类:

public class MariaDBDatabase extends Database {

/*
 * Construct a MySQL database instance.
 *
 * @param host     Host/IP of the MySQL database.
 * @param port     Port of the MySQL database.
 * @param database Database wanted to be access.
 * @param username Username to be authenticated with.
 * @param password Password for the user authentication.
 * @param plugin   Plugin for the schedulers to be assigned to.
 */
public MariaDBDatabase(String host, int port, String database, String username, String password, JavaPlugin plugin) {
    super("org.mariadb.jdbc.MySQLDataSource", "jdbc:mysql://" + host + ":" + port + "/" + database, username, password, plugin);
}

我的数据库类:

public abstract class Database {

private String           jdbcURL;
private HikariDataSource dataSource;
private JavaPlugin       plugin;

/**
 * Construct a database instance.
 *
 * @param className The class name used to get the driver.
 * @param jdbcURL   A JDBC url to use for connecting.
 * @param username  Username to connect with.
 * @param password  Password to authenticate username.
 * @param plugin    A plugin instance for the schedulers to be assigned to.
 */
public Database(String className, String jdbcURL, String username, String password, JavaPlugin plugin) {
    this.jdbcURL = jdbcURL;
    this.plugin = plugin;

    try {
        Bukkit.getLogger().log(Level.INFO, "Initializing the connection pool ... ");
        Class.forName(className);
    } catch (ClassNotFoundException e) {
        Bukkit.getLogger().log(Level.SEVERE, "Exception when trying to initialize the connection pool",e);
        return;
    }

    HikariConfig config = new HikariConfig();

    config.setDriverClassName(className);
    config.setJdbcUrl(jdbcURL);

    config.setUsername(username);
    config.setPassword(password);

    //config.setLeakDetectionThreshold(10000);
    //config.setMaximumPoolSize(10);

    config.setMaximumPoolSize(5);
    config.setConnectionTestQuery("SELECT 1");
    config.setPoolName("HikariCP");

    try {
        dataSource = new HikariDataSource(config);
        Bukkit.getLogger().log(Level.INFO, "Connection pool initialized successfully.");
    } catch (Exception e) {
        Bukkit.getLogger().log(Level.SEVERE, "Exception when trying to initialize the connection pool",e);
    }
}

/**
 * Connects the data pool to the database.
 */
public boolean connect() {
    return isConnected();
}

/**
 * Disconnects (shutdown) the data pool and all connections.
 */
public void disconnect() {
    dataSource.shutdown();
}

/**
 * Query the database and return a cached result.
 *
 * @param query The statement to be queried.
 * @return      Cached rowset returned from query.
 */
public CachedRowSet query(final PreparedStatement preparedStatement) {
    CachedRowSet rowSet = null;

    if (isConnected()) {
        try {
            ExecutorService exe = Executors.newCachedThreadPool();

            Future<CachedRowSet> future = exe.submit(new Callable<CachedRowSet>() {
                public CachedRowSet call() {
                    try {
                        ResultSet resultSet = preparedStatement.executeQuery();

                        CachedRowSet cachedRowSet = new CachedRowSetImpl();
                        cachedRowSet.populate(resultSet);
                        resultSet.close();

                        preparedStatement.getConnection().close();

                        if (cachedRowSet.next()) {
                            return cachedRowSet;
                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }

                    return null;
                }
            });

            if (future.get() != null) {
                rowSet = future.get();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }

    return rowSet;
}

/**
 * Execute a query
 *
 * @param preparedStatement query to be executed.
 */
public void execute(final PreparedStatement preparedStatement) {
    if (isConnected()) {
        Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
            public void run() {
                try {
                    preparedStatement.execute();

                    preparedStatement.getConnection().close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

/**
 * Prepare a statement
 *
 * @param query Query to be prepared.
 * @param vars  Variables to be replaced from ?.
 * @return      a prepared statement.
 */
public PreparedStatement prepareStatement(String query, Object... vars) {
    try {
        PreparedStatement preparedStatement = getConnection().prepareStatement(query);

        int x = 0;

        if (query.contains("?") && vars.length != 0) {
            for (Object var : vars) {
                x++;
                if (var instanceof String) {
                    preparedStatement.setString(x, (String) var);
                } else {
                    preparedStatement.setInt(x, (Integer) var);
                }
            }
        }

        return preparedStatement;
    } catch (SQLException e) {
        e.printStackTrace();
    }

    return null;
}

/**
 * Get a connection from the data pool
 *
 * @return a connection.
 */
public Connection getConnection() {
    try {
        return dataSource.getConnection();
    } catch (SQLException e) {
        e.printStackTrace();
    }

    return null;
}

/**
 * Check if the data pool is connected.
 *
 * @return connected Whether the data pool is connected or not.
 */
public boolean isConnected() {
    try {
        dataSource.getConnection();
    } catch (SQLException e) {
        return false;
    }

    return true;
}

我的家伙.xml

<project
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>eu.elraro.dubilets</groupId>
<artifactId>Dubilets</artifactId>
<version>1.0-SNAPSHOT</version>
<repositories>
    <repository>
        <id>bungeecord-repo</id>
        <url>https://oss.sonatype.org/content/repositories/snapshots</url>
    </repository>
</repositories>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <createDependencyReducedPom>false</createDependencyReducedPom>
                <artifactSet>
                    <includes>
                        <include>org.slf4j:*</include>
                        <include>com.zaxxer:*</include>
                        <include>org.mariadb.jdbc:*</include>
                        <include>org.javassist:javassist</include>
                    </includes>
                </artifactSet>
            </configuration>
        </plugin>
    </plugins>
</build>
<dependencies>
    <!--Spigot API -->
    <dependency>
        <groupId>org.spigotmc</groupId>
        <artifactId>spigot-api</artifactId>
        <version>1.8.8-R0.1-SNAPSHOT</version>
        <scope>provided</scope>
    </dependency>
    <!--Bukkit API -->
    <dependency>
        <groupId>org.bukkit</groupId>
        <artifactId>bukkit</artifactId>
        <version>1.8.8-R0.1-SNAPSHOT</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.zaxxer</groupId>
        <artifactId>HikariCP</artifactId>
        <version>2.4.1</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.mariadb.jdbc</groupId>
        <artifactId>mariadb-java-client</artifactId>
        <version>1.2.3</version>
    </dependency>
    <dependency>
        <groupId>com.gmail.filoghost</groupId>
        <artifactId>holographic-displays-api</artifactId>
        <version>2.1.7</version>
        <scope>system</scope>
        <systemPath>${basedir}/lib/HolographicDisplaysAPI_v2.1.7.jar</systemPath>
    </dependency>
</dependencies>

共有1个答案

陈富
2023-03-14

不要同时设置dataSourceClassname和jdbcUrl...它们是互斥的。为了简单起见,我推荐jdbcUrl。

 类似资料:
  • 我正在开发一个在Java服务器上运行的游戏。对于数据库池,我使用的是HikariCP,这是一个优秀的库,但它现在抛出了以下错误: 现在我知道连接泄漏意味着打开的连接在某个地方漂浮,但我不知道如何或在哪里漂浮。 下面是我的数据库类中的一个方法,根据堆栈跟踪,错误应该发生在这里。 这只是启动语句的一个基本方法。调用它时,我使用它,然后调用、和 但它告诉我连接是打开的。 我怎么解决这个?谢了!

  • 我们的项目中有Spring-boot/hibernate/PostgreSQL应用程序,并使用Hikari作为连接池。我们不断遇到以下问题:几个小时后,活动连接数增长到极限,我们得到如下错误(完整堆栈跟踪位于问题的末尾): 以下是版本信息: JPA/Hibernate配置: HikariCP配置: 完整堆栈跟踪:

  • 我正在使用hikaricp(这可能也适用于任何其他数据库连接池)。我有一个DBPool类,在其中我实例化了一个HikariDataSource(使用HikariConfig对象)。对于这个DBPool,我使用lazyholder习惯用法来限制每个VM一个池实例。但是,一旦获得对池的引用,就可以检索连接对象(无需任何进一步的锁/同步/信号量检查),因为我认为连接池会处理我的连接对象限制。每次通过数据

  • 我有这个使用HikariCP连接池的代码: 我通过发出命令“Show Processlist”来监控mysql中的连接,我看到在行之后创建了100个连接: 。。。正在运行。我肯定这不是命中注定的,对吧?它应该在稍后执行pooledDataSource时创建连接。getConnection()。 我做错了什么?为什么它会立即创建100个连接??

  • 我有一个Spring Boot(v2.0.8)应用程序,它使用HikariCP(v2.7.9)池(连接到MariaDB)配置: 问题在于,我们的生产组件每隔几周就会反复抛出 。问题在于它永远不会从中恢复,并且会不断引发异常。因此,需要重新启动计算装置。 从HikariPool源代码来看,这似乎正在发生,因为每次它调用poolEntry都是空的,因此会抛出超时异常。要使其为空,连接池必须没有空闲条目

  • 地狱, 我是否可以获取HKARIP连接池度量信息,如总连接数、空闲连接数等? 我知道Hikaripool记录这样的信息: 清理前池统计数据库(总计=20,使用次数=0,可用次数=20,等待次数=0) 但是它太频繁了,我的代码无法控制它。我想在可配置的时间内记录这些信息,例如1分钟。顺便说一句,我用Scala Slick 3.0