连接在mysql异常后死亡

问题描述 投票:1回答:1

当我从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

2020-05-18 11:49:06,072 () SqlExceptionHelper [WARN]  SQL Error: 1062, SQLState: 23000
2020-05-18 11:49:06,072 () SqlExceptionHelper [ERROR]  Duplicate entry 'MzEyMjZfTmV3c3wzMDI3MDc1MDcwMDB8NDIxMDgwNTM3MzJwcm9k-10178' for key 'PRIMARY'
2020-05-18 11:49:06,073 () RestExceptionHandler [ERROR]  Unhandled exception
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [PRIMARY]; nested exception is org.hibernate.exception.ConstraintViolationException: 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中获得例外?

我使用Java Spring Hibernate MySQL和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);
        throw t;
    }

    // Call a transactional function
    transactionFunction();
}  

}

[我还注意到,在发生异常后,会话似乎无法正确清除,因为我看到其他不相关的请求上存在重复的键错误。 Hibernate仍然尝试保留该实体。但是,我无法在本地计算机上复制它。

 2020-05-18 11:49:06,072 () SqlExceptionHelper [ERROR]  Duplicate entry 'MzEyMjZfTmV3c3wzMDI3MDc1MDcwMDB8NDIxMDgwNTM3MzJwcm9k-10178'

UPDATE

我添加了日志,并且具有重复键错误的连接将始终尝试在其他不相关的请求中插入有问题的行。是因为我不清除会话?请求结束后不应该自动完成吗?

mysql spring hibernate c3p0
1个回答
-1
投票

这是因为实时会话管理不善,或者甚至无法执行所有查询的选项,或者是由于事务隔离级别选择不当

© www.soinside.com 2019 - 2024. All rights reserved.