【问题标题】:`Could not open connection` issue despite using c3p0尽管使用 c3p0,但“无法打开连接”问题
【发布时间】:2017-11-20 10:12:33
【问题描述】:

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

这是我的 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>

这是我的 HibernateUtil 类:

public class HibernateUtil {

private static final SessionFactory sessionFactory;
static {
    try {
        Configuration configuration = new Configuration().configure();
        StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
                .applySettings(configuration.getProperties());
        sessionFactory = configuration.buildSessionFactory(builder.build());

    } catch (Exception ex) {
        throw new ExceptionInInitializerError(ex);
    }
}

public Session openSession() {
    return sessionFactory.openSession();
}
}

我已将 c3p0 调试配置添加到我的应用程序中,以剔除未返回的连接(以防内存泄漏)并为其生成堆栈跟踪,但日志中没有显示任何内容。

这是今天早上的一些日志:

https://pastebin.com/MGb4Miau

这里有人可以帮我找出问题所在吗?

编辑CustomDialect类:

public class CustomDialect extends MySQL5InnoDBDialect {
public String getTableTypeString() {
    return " ENGINE=InnoDB DEFAULT CHARSET=utf8";
    }
}

【问题讨论】:

    标签: java hibernate c3p0


    【解决方案1】:

    出现此问题是因为您用完了数据库连接。

    1. 保持连接的查询太慢。
    2. 您有很多连接需求无法满足最大池大小100,这是预期的原因,因为问题在运行一天后出现。
    3. 或者你有源泄漏,因为你在以太交易成功或失败后没有关闭连接。

    从 C3P0 的调试日志中尝试查看请求了多少连接。

    还有

    理想情况下,您不得在生产环境中使用unreturnedConnectionTimeout,因此您必须调试连接泄漏,并在没有更多泄漏时移除 unreturnedConnectionTimeoutdebugUnreturnedConnectionStackTraces 配置。


    编辑

    试试这些配置:

    <property key="hibernate.connection.characterEncoding">UTF-8</property>
    <property key="hibernate.connection.useUnicode">true</property>
    

    因为有时 Hibernate 中的编码与 MySQL db 中的编码不同。

    【讨论】:

    • Ebraheem,我设置了 unreturnedConnectionTimeoutdebugUnreturnedConnectionStackTraces 配置以找出连接泄漏。到目前为止,日志上没有显示任何内容。此外,我使用 SonarQube 扫描了我的源代码,也没有检测到资源泄漏。令我惊讶的是,尽管使用了 c3p0 和 unreturnedConnectionTimeoutdebugUnreturnedConnectionStackTraces 配置,但连接不活动时间长达 46,956,212 毫秒或 ~13 小时。
    • 另外,从服务器成功接收到的最后一个数据包是在大约 13 小时之前,我想问一下应用程序什么时候开始运行
    • 您能否在config.CustomDialect 中显示您用作hibernate.dialect 属性的代码。
    • 我已经添加了CustomDialect 类代码以及@Ebraheem
    • 尝试使用org.hibernate.dialect.MySQLInnoDBDialect而不使用5,而不是config.CustomDialect,并检查问题是否仍然出现
    【解决方案2】:

    我终于找到了解决问题的办法。我对我的hibernate.cfg.xml 进行了以下更改:

     <property name="hibernate.c3p0.maxIdleTime">10800</property>
     <property name="hibernate.c3p0.maxIdleTimeExcessConnections">600</property>
    

    而且它有效! maxIdleTime 属性从池中删除空闲时间超过指定时间段(以秒为单位)的连接,确保我的连接不时刷新,maxIdleTimeExcessConnections 让我从池中剔除连接超过指定时间段(以秒为单位)空闲的超过 minPoolSize。这样,我可以确保池中没有太多连接,并且它们都是新鲜的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-19
      • 2010-10-10
      • 2011-02-21
      • 2021-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多