代码之家  ›  专栏  ›  技术社区  ›  Akshay Singh Rinoy Ashokan

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

  •  1
  • Akshay Singh Rinoy Ashokan  · 技术社区  · 8 年前

    Could not open connection 错误然后我会重新启动我的应用程序,以消除这个问题。我无法找出问题的根源。

    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>
    

    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";
        }
    }
    
    2 回复  |  直到 8 年前
        1
  •  0
  •   Ebraheem Alrabeea    8 年前

    出现此问题的原因是数据库连接不足。

    1. 保持连接的查询太慢。
    2. 您有很多连接需求,无法满足最大池大小 100 ,这是预期的原因,因为问题在运行一天后出现。

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

    而且

    unreturnedConnectionTimeout 在生产中,您必须调试连接泄漏,当没有更多泄漏时,将其移除 二者都 未返回的连接超时 debugUnreturnedConnectionStackTraces 配置。


    编辑

    尝试以下配置:

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

        2
  •  0
  •   Akshay Singh Rinoy Ashokan    8 年前

    我终于想出了解决这个问题的办法。我对我的 hibernate.cfg.xml :

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

    这是可行的!这个 maxIdleTime 属性从池中删除空闲时间超过指定时间段(以秒为单位)的连接,以确保我的连接会不时刷新,并且 maxIdleTimeExcessConnections 让我从池中剔除超过 minPoolSize