简体   繁体   English

MySQL:EOFException:无法读取来自服务器的响应。 预期读取4个字节,在连接意外丢失之前读取0个字节

[英]MySQL : EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost

I have an Spring application with quartz scheduler for batch processing. 我有一个带有石英调度程序的Spring应用程序,用于批处理。 It was working fine from last 4 years. 最近4年以来一切正常。 There is a batch which runs on every 1 minute. 每1分钟运行一次批处理。 But from last few days suddenly when the batch is trying to fetch some data we are getting org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Could not open connection 但是从最近几天开始,当该批处理试图获取某些数据时,我们突然收到org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Could not open connection org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Could not open connection

This batch runs throughout the day but some times gives this exception and all other batches fails to connect to MySQL. 该批次全天运行,但有时会出现此异常,而所有其他批次均无法连接到MySQL。 So we are restarting the application and it is working fine. 因此,我们正在重新启动该应用程序,它运行正常。

Data source config 数据源配置

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="url">
            <value>${DATABASE_URL}</value>
        </property>
        <property name="driverClassName">
            <value>com.mysql.jdbc.Driver</value>
        </property>
        <property name="username">
            <value>${DATABASE_USER}</value>
        </property>
        <property name="password">
            <value>${DATABASE_PASSWORD}</value>
        </property>
</bean>

Session factory connection pooling configuration 会话工厂连接池配置

<bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.enable_lazy_load_no_trans">true</prop>
<!-- Configurations specific to c3p0 connection pooling -->

<prop key="hibernate.c3p0.acquire_increment">5</prop>
<prop key="hibernate.c3p0.idle_test_period">1800</prop>
<prop key="hibernate.c3p0.max_size">600</prop>
<prop key="hibernate.c3p0.max_statements">50</prop>
<prop key="hibernate.c3p0.min_size">5</prop>
<prop key="hibernate.c3p0.timeout">1800</prop>
</props>
</property>
<property name="annotatedClasses">
<list> .... </list>
</property>
</bean>

May be it is the problem with the mysql running your db-server. 可能是运行您的数据库服务器的mysql问题。

change the mysql configuration in your db-server 更改数据库服务器中的mysql配置

add the bind-address in mysql.cnf under [mysqld] section 在[mysqld]部分的mysql.cnf中添加绑定地址

bind-address=your_spring_application_ip

also change the accessing host for the mysql user. 还更改mysql用户的访问主机。 run the following query in mysql 在mysql中运行以下查询

update mysql.user set host='%' where user='your_username';
flush privileges;

then restart the mysql service 然后重启mysql服务

sudo systemctl restart mysql

Recently we had kind of the same issue. 最近,我们遇到了同样的问题。 Our application failed to connect to aws Aurora instance. 我们的应用程序无法连接到AWS Aurora实例。 What we ended up doing changing sql driver to mariadb one. 我们最终将sql驱动程序更改为mariadb。 Hope this will help. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM