Java `尽管使用c3p0,但无法打开连接`问题
几个月来,我一直在Spring Boot应用程序中使用c3p0进行连接池。一切都很好,直到大约两周前,我开始遇到连接问题,尤其是在早上。每天早上当我尝试登录到我的应用程序时,它都会抛出一个Java `尽管使用c3p0,但无法打开连接`问题,java,hibernate,c3p0,Java,Hibernate,C3p0,几个月来,我一直在Spring Boot应用程序中使用c3p0进行连接池。一切都很好,直到大约两周前,我开始遇到连接问题,尤其是在早上。每天早上当我尝试登录到我的应用程序时,它都会抛出一个无法打开连接错误。然后我会重新启动我的应用程序,以消除这个问题。我无法找出问题的根本原因 下面是我的hibernate.cfg.xml: hibernate-configuration> <session-factory> <property name="hibernate.co
无法打开连接
错误。然后我会重新启动我的应用程序,以消除这个问题。我无法找出问题的根本原因
下面是我的hibernate.cfg.xml:
hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mydb?autoReconnect=true</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">abc123</property>
<property name="hibernate.dialect">config.CustomDialect</property>
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.initialPoolSize">5</property>
<property name="hibernate.c3p0.minPoolSize">5</property>
<property name="hibernate.c3p0.maxPoolSize">100</property>
<property name="hibernate.c3p0.checkoutTimeout">3000</property>
<property name="hibernate.c3p0.maxStatementsPerConnection">30</property>
<property name="hibernate.c3p0.unreturnedConnectionTimeout">3000</property>
<property name="hibernate.c3p0.debugUnreturnedConnectionStackTraces">true</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
...
POJO mappings
</session-factory>
</hibernate-configuration>
我已将c3p0调试配置添加到我的应用程序中,以剔除未返回的连接(在内存泄漏的情况下),并为其生成堆栈跟踪,但日志中没有显示任何内容
以下是今天早上的一些日志:
这里有人能帮我找出问题所在吗
编辑:自定义方言
类:
public class CustomDialect extends MySQL5InnoDBDialect {
public String getTableTypeString() {
return " ENGINE=InnoDB DEFAULT CHARSET=utf8";
}
}
出现此问题是因为数据库连接不足
100
,这是预期的原因,因为问题在运行一天后出现李>
unreturnedConnectionTimeout
,因此必须调试连接泄漏,并在没有更多泄漏时删除
unreturnedConnectionTimeout
和debugUnreturnedConnectionStackTraces
config
编辑 尝试以下配置:
<property key="hibernate.connection.characterEncoding">UTF-8</property>
<property key="hibernate.connection.useUnicode">true</property>
UTF-8
真的
因为有时候Hibernate中的编码与MySQL db中的编码不同。出现此问题是因为数据库连接不足
100
,这是预期的原因,因为问题在运行一天后出现李>
unreturnedConnectionTimeout
,因此必须调试连接泄漏,并在没有更多泄漏时删除
unreturnedConnectionTimeout
和debugUnreturnedConnectionStackTraces
config
编辑 尝试以下配置:
<property key="hibernate.connection.characterEncoding">UTF-8</property>
<property key="hibernate.connection.useUnicode">true</property>
UTF-8
真的
因为有时候Hibernate中的编码与MySQL db中的编码不同。我终于找到了解决问题的方法。我对我的
hibernate.cfg.xml
做了以下更改:
<property name="hibernate.c3p0.maxIdleTime">10800</property>
<property name="hibernate.c3p0.maxIdleTimeExcessConnections">600</property>
10800
600
而且它有效!
maxIdleTime
属性从池中删除空闲时间超过指定时间段(以秒为单位)的连接这确保了我的连接会不时刷新,maxIdleTimeExcessConnections
允许我从池中剔除空闲时间超过指定时间段(秒)的连接。这样,我可以确保池中没有太多的连接,并且它们都是新的。我终于找到了解决问题的方法。我对我的hibernate.cfg.xml
做了以下更改:
<property name="hibernate.c3p0.maxIdleTime">10800</property>
<property name="hibernate.c3p0.maxIdleTimeExcessConnections">600</property>
10800
600
而且它有效!
maxIdleTime
属性从池中删除空闲时间超过指定时间段(以秒为单位)的连接这确保了我的连接会不时刷新,maxIdleTimeExcessConnections
允许我从池中剔除空闲时间超过指定时间段(秒)的连接。通过这种方式,我可以确保池中没有太多的连接,并且它们都是新的。Ebraheem,我已经设置了unreturnedConnectionTimeout
和debugUnreturnedConnectionStackTraces
config,以找出连接泄漏。到目前为止,日志上没有显示任何内容。此外,我已经使用SonarQube扫描了我的源代码,它也没有检测到任何资源泄漏。我发现令人惊讶的是,尽管使用c3p0和unreturnedConnectionTimeout
和debugUnreturnedConnectionStackTraces
config,但仍会出现高达46956212 ms或~13小时的连接不活动。此外,从服务器成功接收到的最后一个数据包大约在13小时之前,我想问一下应用程序什么时候开始运行,你能在config.customdialent
中显示你用作hibernate.dialent
属性的代码吗?我已经添加了customdialent
类代码以及@Ebraheemtry来使用org.hibernate.dialen.mysqlinnodbdialent
而不使用5
,我设置了unreturnedConnectionTimeout
和debugUnreturnedConnectionStackTraces
config,而不是config.customdialent
,并检查问题是否仍然存在。我设置了unreturnedConnectionTimeout
和debugUnreturnedConnectionStackTraces
config,以找出连接泄漏。到目前为止,日志上没有显示任何内容。此外,我已经使用SonarQube扫描了我的源代码,它也没有检测到任何资源泄漏。我发现令人惊讶的是,尽管使用c3p0和unreturnedConnectionTimeout
和debugUnreturnedConnectionStackTraces
config,但仍会出现高达46956212 ms或~13小时的连接不活动。此外,从服务器成功接收到的最后一个数据包大约在13小时之前,我想问一下应用程序什么时候开始运行,你能在config.customdial
中显示你用作hiberna的代码吗