几分钟后MySQL数据库连接断开连接

时间:2014-06-27 07:27:55

标签: java mysql spring hibernate jsf

我正在使用Spring + Hibernate + JSF,但如果我在页面上理想的几分钟,我在尝试一些数据库查询时会遇到异常

 The last packet successfully received from the server was 2,615,049 milliseconds ago.  The last packet sent successfully to the server was 27 milliseconds ago.

Caused by: org.hibernate.TransactionException: JDBC begin transaction failed: 
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:76)
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:160)
    at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1309)
    at org.springframework.orm.hibernate4.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:474)
    ... 46 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

此问题是由于我的项目中的任何配置或其数据库供应商问题造成的 在web.xml中我做了这个条目

<session-config>
   <session-timeout>
          30
   </session-timeout>
 </session-config>

连接池

<bean id="DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUrl" value="jdbc:mysql://IP:3306/ccc" />
                <property name="user" value=" hariom" />
                <property name="password" value="password" />
        <property name="maxPoolSize" value="2" />
        <property name="maxStatements" value="0" />
        <property name="minPoolSize" value="1" />
</bean>

1 个答案:

答案 0 :(得分:1)

您应该添加此属性;

<property name="validationQuery" value="SELECT 1" />

这将验证连接,如果mysql关闭了连接,您的应用将不会尝试发送数据包。