gpt4 book ai didi

mysql - 与 Hibernate 的连接过多异常

转载 作者:行者123 更新时间:2023-11-29 07:56:23 25 4
gpt4 key购买 nike

当我尝试连接 Hibernate 时出现错误。该代码创建 1000 个线程,每个线程打开一个连接。不管怎样,代码非常短,而且它们很快就关闭了连接。

我打印一个包含连接总数的计数器(当我打开一个连接时增加,当我关闭一个连接时减少......)。当我得到大约 155 个连接时,我收到该错误。我尝试将池连接(默认值为 150)增加到 500,但在 180 个连接时出现相同的错误。

我做了另一个小测试,在启动每个线程后我执行 Thread.sleep(10) 并且它有效,我不知道这就是为什么如果我说我的池大小为 500,它就不起作用。

for (int i=0; i<1000; i++) {
Thread.sleep(10); //it works with this modification because it gets time to finish some threads..
newThread = new WThread(params);
newThread.start();
}

池配置:

<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.connection.autocommit">false</prop>
<prop key="hibernate.show_sql">false</prop>

<prop key="hibernate.hbm2ddl.auto">validate</prop>

<prop key="hibernate.c3p0.min_size">2</prop>
<prop key="hibernate.c3p0.max_size">125</prop> --> increment this parameter it's not working.
<prop key="hibernate.c3p0.timeout">1800</prop>
<prop key="hibernate.c3p0.max_statements">500</prop>
</props>

我不知道这可能是什么,,,我电脑的内存?我不知道。

我得到的错误:

14/07/30 09:57:59 WARN spi.SqlExceptionHelper: SQL Error: 1040, SQLState: 08004
14/07/30 09:57:59 ERROR spi.SqlExceptionHelper: Data source rejected establishment of connection, message from server: "Too many connections"

...

14/07/30 09:57:59 INFO internal.DefaultLoadEventListener: HHH000327: Error performing load command : org.hibernate.exception.JDBCConnectionException: Could not open connection
14/07/30 09:57:59 WARN spi.SqlExceptionHelper: SQL Error: 1040, SQLState: 08004

最佳答案

增加 hibernate.c3p0.max_size 没有帮助,因为 MySQL 数据库没有提供更多连接。如documented , max_connections 的默认值为 151:

The number of connections permitted is controlled by the max_connections system variable. The default value is 151 to improve performance when MySQL is used with the Apache Web server.

设置该变量立即生效,无需重启:

    set global max_connections = 500;

要在重新启动后也保留此设置,应将以下内容添加到 my.cnf:

    max_connections = 500

关于mysql - 与 Hibernate 的连接过多异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25030298/

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