gpt4 book ai didi

java - MySQL 连接池超过 Java 中的最大连接数

转载 作者:行者123 更新时间:2023-11-29 04:07:30 24 4
gpt4 key购买 nike

我有一个远程 MySQL 数据库。在 Java 中,我有一个连接池:

pool = new MysqlConnectionPoolDataSource();
pool.setURL("jdbc:mysql://1.2.3.4:3306/TEST?max-connections=100");
pool.setUser("USER");
pool.setPassword("PASSWORD");

最大连接数是 100。现在让我们创建 100 个线程:

for (int i = 0; i < 100; ++i) {
Thread t = new Thread(new Client(i, pool));
t.start();
}

这是在每个线程中运行的代码:

class Client implements Runnable {
int id;
MysqlConnectionPoolDataSource pool;
public Client(int id, MysqlConnectionPoolDataSource pool) {
this.id = id;
this.pool = pool;
}
public void run() {
while (true) {
try {
Connection conn = pool.getConnection();
ResultSet set = conn.createStatement().executeQuery("SELECT * FROM SOMETHING");
if (set.next()) {
System.out.println(id + " finished.");
}
set.close();
conn.close();
return;
}catch (Exception e) {
System.out.println("ERROR " + id + " -> " + e.getMessage());
}
}
}
}

所以,我有一个最大连接数为 100 的池,然后有 100 个线程尝试每个使用一个连接。

然而,有几个形式的错误:

ERROR 30 -> User already has more than 'max_user_connections' active connections

但为什么最大连接数是 100?

其实就算改成

pool.setURL("jdbc:mysql://1.2.3.4:3306/TEST?max-connections=300");

问题仍然存在。

当然,最终会建立连接,我可以执行查询。但我不明白为什么我应该超过连接数。

最佳答案

max_user_connectionsmy.ini 中的一个 server 设置:参见 here .与连接池、Java等无关。

关于java - MySQL 连接池超过 Java 中的最大连接数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27138175/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com