我在分布式系统中工作,该系统具有多个服务器,与前端 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&useUnicode=true&characterEncoding=cp1251&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/