mysql异常后连接终止

mysql异常后连接终止,mysql,spring,hibernate,c3p0,Mysql,Spring,Hibernate,C3p0,当我从mysql得到一个异常时,比如下面的异常 19:36:35,712 WARN [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] - SQL Error: 1205, SQLState: 40001 19:36:35,713 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] - Lock wait timeout exceeded; try restarting tran

当我从mysql得到一个异常时,比如下面的异常

19:36:35,712 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper]  - SQL Error: 1205, SQLState: 40001
19:36:35,713 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper]  - Lock wait timeout exceeded; try restarting transaction
org.springframework.dao.PessimisticLockingFailureException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.PessimisticLockException: could not execute statement
该特定连接处于错误状态,10分钟后,我出现以下错误

19:48:41,859 WARN  [com.mchange.v2.c3p0.impl.NewPooledConnection]  - [c3p0] A PooledConnection that has already signalled a Connection error is still in use!
19:48:41,859 WARN  [com.mchange.v2.c3p0.impl.NewPooledConnection]  - [c3p0] Another error has occurred [ java.sql.SQLNonTransientConnectionException: No operations allowed after connection closed. ] which will not be reported to listeners!

Caused by: com.mysql.cj.exceptions.CJCommunicationsException: The last packet successfully received from the server was 716,308 milliseconds ago. The last packet sent successfully to the server was 716,308 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中实现同样的功能

我使用JavaSpringHibernateMySQL和c3p0

这是配置

hibernate.dialect=org.hibernate.dialect.MySQL57Dialect
hibernate.hbm2ddl.delimiter=;
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=250
hibernate.c3p0.checkout_timeout=120000
hibernate.c3p0.test_connection_on_checkin=true
hibernate.c3p0.idle_connection_test_period=300
hibernate.archive.autodetection=class
hibernate.hbm2ddl.auto=validate
代码要么在@Transactional函数中

    @Override
    @Transactional(
            rollbackFor = Exception.class)
    public Long create() {
       //Hibernate and queryDsl code
    }
或者有时我不得不像这样手动管理事务,因为如果最后一个函数失败,我不想回滚。我认为这是导致问题的功能

public void function(){
try {
    final TransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
    final TransactionStatus transactionStatus = platformTransactionManager.getTransaction(transactionDefinition);
    try {
        //Hibernate and QueryDsl code

        entityManager.flush();
        platformTransactionManager.commit(transactionStatus);
    } catch(Throwable t) {
        LOG.error(t);
        platformTransactionManager.rollback(transactionStatus);
        entityManager.clear();
        throw t;
    }
    // Call a transactional function
    transactionFunction();
}  
}

编辑


删除了关于重复密钥错误的部分,因为我现在可以确认它与问题无关。

这是因为管理不好的实时会话或执行所有操作的选项,甚至是查询,或者因为事务隔离级别选择不当。

请尝试更改此设置: -等待超时默认值28.800秒。 -交互式超时默认值为28.800秒。 -innodb\u锁定\u等待\u超时默认值50秒。如果您使用的是INNODB


p、 s:第二个错误是关于数据中的重复条目,在您用作主键的字段中,可能您需要检查发送到mysql的重复数据

我认为您应该删除配置文件中的每一行,除了方言和runSpring从c3p0移动到HikariCp,你能考虑更改连接池吗?试着在脚本中清除会话。@ PATEDARMIS,我确实调用了EntIdMaCurror。但是在等待超时之后,连接仍然处于不好的状态。