java - C3P0 设置不明确

标签 java mysql hibernate connection-pooling c3p0

我在分布式系统中工作,该系统具有多个服务器,与前端 MySQL 数据库一起使用,并在不同的查询中复制数据库。 多台服务器使用最多 1000 个连接的复合(700 - 800 个,困难时期为 900 个)。

我偶尔会遇到 MySQL 关闭连接的问题,并看到已经很无聊的异常:

The last packet sent successfully to the server was 65,179,696 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.

MySQL wait_timeout 设置为默认值,等于 28800(8 小时)

我使用 C3P0 与 hibernate 配对来服务我与 MySQL 的连接,并在所有 hibernate-custom*.xml 中的所有服务器上进行下一个 C3P0 设置:

    <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
    <property name="hibernate.current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</property>
    <property name="hibernate.connection.url">jdbc:mysql://xxx.xxx.xxx.xxx:3306/ibaserver?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=cp1251&amp;zeroDateTimeBehavior=convertToNull</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.username">username</property>
    <property name="hibernate.connection.password">********</property>
    <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
    <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>

    <property name="hibernate.c3p0.min_size">5</property>
    <property name="hibernate.c3p0.max_size">50</property>
    <property name="hibernate.c3p0.timeout">1800</property>
    <property name="hibernate.c3p0.max_statements">0</property>
    <property name="hibernate.c3p0.idle_test_period">59</property>
    <property name="hibernate.c3p0.acquire_increment">3</property>
    <property name="hibernate.c3p0.preferredTestQuery">select 1</property>

    <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
    <property name="hibernate.cache.use_query_cache">false</property>
    <property name="hibernate.cache.use_second_level_cache">false</property>

有人可以回答我做错了什么吗?为什么会定期失去连接

最佳答案

在从池中借用时使用验证查询以避免MySQL关闭您的连接异常。

更新

如果没有一些额外的配置,您定义的验证查询就无法运行,这些应该足够了: hibernate.c3p0.validate=true hibernate.c3p0.testConnectionOnCheckout=true

关于java - C3P0 设置不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24264350/

相关文章:

java - JPA 的 commit() 方法是否使实体分离?

mysql - 当IN为空时如何在mysql中不显示任何行

c# - C# wpf中用datagrid信息填充MYSQL数据库

sql - MySQL连接问题

java - 在我的 Spring Hibernate 项目上获取 InvokingTargetException

mysql - 使用聚合函数时从 hibernate 中的多个表中选择列

java - 在 AJAX 请求期间处理来自服务器的客户端组件

java - 是否可以获取 Hibernate sqlRestriction 的连接表的 SQL 别名?

java - 用于执行 Java-jQuery 对象远程处理的库或组件?

nhibernate - 将 Hibernate 过滤器属性应用于具有多对多关系的 Bag