JDBC MYSQL too many connections 两种解决方案

在这里插入图片描述

一、问题描述


使用java api创建mysql连接时,报错

too many connections

例如:工程中创建连接

        //读取配置
        Properties properties = new Properties();
        try {
            properties.load(new InputStreamReader(Objects.requireNonNull(Test.class.getClassLoader().getResourceAsStream("clusterKafkaConfigs.properties")), StandardCharsets.UTF_8));
        } catch (IOException e) {
            e.printStackTrace();
        }

        //创建mysql连接
        try {
            conn = MysqlUtils.connect(properties.get("mysql_host").toString(),
                    properties.get("mysql_port").toString()
                    , properties.get("mysql_user").toString(),
                    properties.get("mysql_password").toString());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

二、解决方案1

    show variables like "max_connections"; `

  show global variables like 'wait_timeout'; 

使用上述命令在mysql命令行可查出,mysql默认最大连接数是200,连接等待时长是8小时,显然,如果你的连接数大于200,就会报错,连接太多。

因此你可以去mysql的配置文件中司改这两个参数,max_connections=10000,wait_timeout=200ms.

设置完,重启mysql,就可以解决。


解决方案2(终极解决方案)

方案一显然治标不治本,不过应对一般的场景足矣。
将mysql的连接创建放到静态语句块中,这样多次创建对象,就只创建一个连接,从根本上解决连接过多的问题!

   static{


        //读取配置
        Properties properties = new Properties();
        try {
            properties.load(new InputStreamReader(Objects.requireNonNull(Test.class.getClassLoader().getResourceAsStream("clusterKafkaConfigs.properties")), StandardCharsets.UTF_8));
        } catch (IOException e) {
            e.printStackTrace();
        }

        //创建mysql连接
        try {
            conn = MysqlUtils.connect(properties.get("mysql_host").toString(),
                    properties.get("mysql_port").toString()
                    , properties.get("mysql_user").toString(),
                    properties.get("mysql_password").toString());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

附录:(讲一讲静态语句快的原理,执行下面代码你就可以彻底理解为什么放到静态语句快中就可以解决连接过多的问题!)

public class test {
    public static void main(String[] args) {

        A a1 = new A();
        A a2 = new A();


    }
}

class A{
    static{
        System.out.println("静态块");
    }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MySQL "too many connections"错误是由于达到了MySQL的最大连接数限制而导致的。您可以通过以下几个步骤来解决这个问题: 1. 首先,您可以使用命令`show variables like '%max_connections%';`来查看MySQL允许的最大连接数。 这个值通常是由MySQL配置文件中的max_connections参数设置的,默认情况下为150。 2. 您还可以使用命令`show status like 'Max_used_connections';`来查看MySQL在当前运行期间的最高连接数。 如果这个值接近或等于最大连接数限制,那么可能是因为您的应用程序或数据库负载导致了太多的连接请求。 3. 如果您发现MySQL的最大连接数设置较低,并且您的应用程序需要更多的连接数来处理并发请求,您可以尝试增加最大连接数的限制。可以通过修改MySQL配置文件(my.cnf)中的max_connections参数来实现。 但是请注意,增加最大连接数也会增加系统资源的使用量,因此需要谨慎地评估您的服务器的硬件和资源限制。 4. 另外,您还可以通过优化您的应用程序和数据库查询来减少连接数。这可以包括使用连接池技术来管理数据库连接,以及优化代码和查询以降低数据库负载。 综上所述,解决MySQL "too many connections"错误的方法包括增加最大连接数限制、优化应用程序和数据库查询,以及使用连接池等技术来管理数据库连接。请根据您的具体情况选择适合的解决方案。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Mysql报错:too many connections原因及解决方法](https://blog.csdn.net/B001XFX/article/details/132172241)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [MySQL:Too many connections如何解决](https://blog.csdn.net/zhizhengguan/article/details/122083507)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海若[MATRIX]

鼓励将是我最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值