Hibernate JDBCExceptionReporter -> Connection validity, timeout, auto reconnect
I have a problem with Hibernate (3.6.0.-Final) in my Java EE application using MySQL and the MySQL Connector/J (official JDBC driver for MySQL).
In my hibernate.cfg.xml
, I have these lines of code:
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/mydbtest</property>
<property name="connection.username">root</property>
<property name="connection.password">mypassword</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">20</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management, in this case
the session will be close after each transaction! -->
<property name="current_session_context_class">thread</prop开发者_C百科erty>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- auto create tables -->
<property name="hbm2ddl.auto">none</property>
...
</session-factory>
</hibernate-configuration>
I changed the value of the property connection.pool_size
from 1 to 20. I did this because someone was telling me (and I had these problems), that the data was not loaded and an error was thrown with connection pool size. Also,
So I changed the value to 20. but now I get this error:
Hibernate: select ...
WARN : org.hibernate.util.JDBCExceptionReporter - SQL Error: 0, SQLState: 08S01
ERROR: org.hibernate.util.JDBCExceptionReporter -
The last packet successfully received from the server was 61.428.729
milliseconds ago. The last packet sent successfully to the server was
61.428. 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.
So what should I do of these possible solutions? What's the best way to solve these JDBC connection issues? I read sth about c3p0
, but I am new to connection pools. I do not understand the problem: I have a connection pool of 20. So why does it fail? Why throwing an exception and not open a new one?
Thank you a lot in advance & Best Regards.
-- Update --
<property name=“hibernate.c3p0.acquire_increment”>3</property>
<property name=“hibernate.c3p0.idle_test_period”>14400</property>
<property name=“hibernate.c3p0.timeout”>25200</property>
<property name=“hibernate.c3p0.max_size”>15</property>
<property name=“hibernate.c3p0.min_size”>3</property>
<property name=“hibernate.c3p0.max_statements”>0</property>
<property name=“hibernate.c3p0.preferredTestQuery”>select 1;</property>
-- Update 2 --
/etc/my.cnf
file:
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
This usually happens after some time of inactivity, and MySQL closed the connection in the server side. But the connection in the client side (your application) is still available, so, it ends up being stale.
By using a connection pooling mechanism, it gets in charge of opening/closing the connections when needed, avoiding this kind of problem. One solution, as the error message states, is to use the JDBC option autoReconnect=true in the URL. The other is to configure your connection pooling mechanism to open/test the connection before delivering to your application.
精彩评论