简体   繁体   English

com.mysql.cj.jdbc.exceptions.CommunicationsException:从服务器成功收到的最后一个数据包是XXXXXXXXXXXX毫秒前

[英]com.mysql.cj.jdbc.exceptions.CommunicationsException: The last packet successfully received from the server was XXXXXXXXXXXX milliseconds ago

I have a spring boot application that does not use connection pool and we didn't want to open a DB connection at every request 我有一个不使用连接池的Spring Boot应用程序,我们不想在每个请求时都打开数据库连接

So, here is what we have in a class called MySQLService which has methods with DB queries: 因此,这就是我们称为MySQLService的类中的内容,该类具有用于DB查询的方法:

    @Autowired
    @Qualifier("mysqlDB")
    private Connection connection;

This connection object is always used in all of the methods with queries. connection对象始终在所有带有查询的方法中使用。

In MySQLConnection class, 在MySQLConnection类中,

 @Bean(name = "mysqlDB")
    public Connection getConnection() {
        Connection connection = null;
        try {
            Class.forName(mysqlDriver);
            LOGGER.debug("get mysql connection...");
            connection = DriverManager
                    .getConnection(jdbcUrl,
                            user, password);
        } catch (Exception exception) {
            LOGGER.error("ERROR :: {}", exception);
                        }    
        return connection;
    }
}

So, we are never really closing the connection, it is being managed by spring context but since we are not using JDBCTemplates, it does not get closed. 因此,我们永远不会真正关闭连接,它是由spring上下文管理的,但是由于我们没有使用JDBCTemplates,因此不会关闭它。 We have autoreconnect set to true in connection string. 我们在连接字符串中将autoreconnect设置为true

In a day or two, we get the exception: 在一两天内,我们会得到一个例外:

com.mysql.cj.jdbc.exceptions.CommunicationsException: The last packet successfully received from the server was 61,183,452 milliseconds ago.

I understand it is because SQL Server has connection lifetime set so it expires the connection but what is a way to handle this without using a connection pool 我了解这是因为SQL Server设置了连接生存期,因此它会使连接过期,但是在不使用连接池的情况下如何处理此问题?

Schedule a ping to the MySQL Server every 6 hours or so, executing this query: select 1 from dual . 每6个小时左右对MySQL服务器进行一次ping调度,执行此查询: select 1 from dual For that, you need to enable scheduling: 为此,您需要启用调度:

@Configuration
@EnableScheduling
public class SpringConfig {
    //...
}

then: 然后:

@Scheduled(cron = "0 */6 * * *")
public void schedulePingMySQL() {
   // execute `select 1 from dual`
}

Anyway, using a connection pool is the recommended way. 无论如何,建议使用连接池。 This case the code may look like: 这种情况下的代码可能如下所示:

 @Autowired
private DataSource dataSource;

public void save (Dto dto) {
    Connection con = dataSource.getConnection();
    // finally, close the connection
}

暂无
暂无

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

相关问题 com.mysql.cj.jdbc.exceptions.CommunicationsException:通信链路故障。 驱动程序没有收到来自服务器的任何数据包 - com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure. The driver has not received any packets from the server com.mysql.cj.jdbc.exceptions.CommunicationsException:通信链接失败 - com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure com.mysql.jdbc.exception.jdbc4.CommunicationsException:成功从服务器接收到的最后一个数据包是 - com.mysql.jdbc.exception.jdbc4.CommunicationsException: The last packet successfully received from the server was Spring MySQL com.mysql.cj.jdbc.exceptions.CommunicationsException:通信链接失败 - Spring MySQL com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure com.mysql.cj.jdbc.exceptions.CommunicationsException:Z05B60653C41A8BDA4E6DFC3AFD31A214E6D中的通信链路故障 - com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure in docker com.mysql.cj.jdbc.exceptions.CommunicationsException:Z9CE3D468890F9087C项目中的通信链路故障 - com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure in JPA project com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure - 仅在 docker 中部署应用程序时失败 - com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure - Fails only when deploying the app in docker CommunicationsException: xxx 毫秒前成功发送到服务器的最后一个数据包 - CommunicationsException: The last packet sent successfully to the server xxx milliseconds ago com.mysql.cj.jdbc.exceptions.CommunicationsException:GCP 中的通信链路故障 - CloudSQL - com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure in GCP - CloudSQL Spring Controller Unit test exception:Caused by: com.mysql.cj.jdbc.exceptions.CommunicationsException - Spring Controller Unit test exception :Caused by: com.mysql.cj.jdbc.exceptions.CommunicationsException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM