Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java `尽管使用c3p0,但无法打开连接`问题_Java_Hibernate_C3p0 - Fatal编程技术网

Java `尽管使用c3p0,但无法打开连接`问题

Java `尽管使用c3p0,但无法打开连接`问题,java,hibernate,c3p0,Java,Hibernate,C3p0,几个月来,我一直在Spring Boot应用程序中使用c3p0进行连接池。一切都很好,直到大约两周前,我开始遇到连接问题,尤其是在早上。每天早上当我尝试登录到我的应用程序时,它都会抛出一个无法打开连接错误。然后我会重新启动我的应用程序,以消除这个问题。我无法找出问题的根本原因 下面是我的hibernate.cfg.xml: hibernate-configuration> <session-factory> <property name="hibernate.co

几个月来,我一直在Spring Boot应用程序中使用c3p0进行连接池。一切都很好,直到大约两周前,我开始遇到连接问题,尤其是在早上。每天早上当我尝试登录到我的应用程序时,它都会抛出一个
无法打开连接
错误。然后我会重新启动我的应用程序,以消除这个问题。我无法找出问题的根本原因

下面是我的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
    ,这是预期的原因,因为问题在运行一天后出现
  • 或者您有源泄漏,因为在事务成功或失败后您没有关闭连接
  • 从调试时的C3P0日志中,尝试查看请求的连接数

    理想情况下,您不能在生产中使用
    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
    ,这是预期的原因,因为问题在运行一天后出现
  • 或者您有源泄漏,因为在事务成功或失败后您没有关闭连接
  • 从调试时的C3P0日志中,尝试查看请求的连接数

    理想情况下,您不能在生产中使用
    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的代码吗