【问题标题】:The last packet successfully received from the server was 43417 seconds ago从服务器成功接收到的最后一个数据包是 43417 秒前
【发布时间】:2017-07-15 05:34:02
【问题描述】:

我们正在使用 Spring 4.2.5、Hibernate 4.1.4、MYSql 来构建 REST 服务。我们面临着非常奇怪的问题,无法理解到底发生了什么并且出现以下错误:

从服务器成功接收的最后一个数据包是 43417 秒前。最后一个成功发送到服务器的数据包是 43417 秒前,比服务器配置的“wait_timeout”值长。您应该考虑在应用程序中使用之前使连接失效和/或测试连接有效性,增加客户端超时的服务器配置值,或使用 Connector/J 连接属性“autoReconnect=true”来避免此问题。

为了获得数据库连接,我们使用基于 java 的配置,如下所示:

    @Configuration
    @EnableTransactionManagement
    @ComponentScan({ "api.configuration" })

public class HibernateConfiguration {
    @Autowired
    private Environment environment;

    private static final Logger logger = LoggerFactory.getLogger(HibernateConfiguration.class);
    @Bean
    public LocalSessionFactoryBean sessionFactory() throws PropertyVetoException {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setPackagesToScan(new String[] { "api.domain" });
        sessionFactory.setHibernateProperties(hibernateProperties());
        return sessionFactory;
     }

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/test?autoReconnect=true");
        dataSource.setUsername("test"); 
        dataSource.setPassword("test");
        return dataSource;
    }


    private Properties hibernateProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
        properties.put("hibernate.show_sql", "true");
        properties.put("hibernate.format_sql", "true");
        properties.put("hibernate.hbm2ddl.auto", "update");
        properties.put("connection.autoReconnect",  "true");
        properties.put("connection.autoReconnectForPools","true");
        properties.put("connection.is-connection-validation-required", "true");

        return properties;        
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws PropertyVetoException {
       LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
       em.setDataSource(dataSource());
       em.setPackagesToScan(new String[] { "api.domain" });

       JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
       em.setJpaVendorAdapter(vendorAdapter);
       em.setJpaProperties(hibernateProperties());

       return em;
    }

    @Bean
    public PlatformTransactionManager transactionManager() throws PropertyVetoException{
       JpaTransactionManager transactionManager = new JpaTransactionManager();
       transactionManager.setEntityManagerFactory(entityManagerFactory().getObject() );
       return transactionManager;
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
       return new PersistenceExceptionTranslationPostProcessor();
    }
}

google了一下,发现我们可以通过添加以下两个属性来解决这个问题:

<property name=”validationQuery” value=”SELECT 1″ />

<property name=”testOnBorrow” value=”true” />

由于我们使用 java 配置而不是基于 XML,我们不知道在 java 类中为 validationQuery 和 testOnBorrow 添加的确切属性名称,因为我们有 hibernate.show_sql

请推荐

【问题讨论】:

    标签: mysql spring hibernate


    【解决方案1】:

    数据库终止了它的会话和物理连接,但客户端直到现在才知道。

    让我们首先了解问题。 java.sql.Connection 是一个 Java 对象,代表一些 MySQL 服务器连接。这是通过 TCP 发生的客户端-服务器通信。如果服务器连接断开或 TCP 连接断开客户端不知道这一点,因此 Java 对象仍然存在,但它无法再与服务器通信。在您的情况下,如果过时连接空闲一段时间(默认为 8 小时),MySQL 服务器会终止陈旧连接。

    如果您创建一个裸驱动程序连接,这种情况将非常罕见 - 此类连接会在短时间使用后创建并关闭。在您的示例中,事情非常混乱-您同时使用了 SessionFactory 和 EntityManagerFactory 并且看起来您使用了裸连接(而且裸连接很难出现此错误),所以我不会对此发表评论..

    您找到的选项不适用于 Hibernate。它们用于生产就绪的数据库池之一(我假设Tomcat JDBC Pool)。因此,您应该使用该池(或另一个像 C3P0 之类的池)并将其配置为定期检查连接 - 这样从 MySQL 服务器的角度来看,连接不会过时。以下是C3P0 配置的示例:

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="jdbcUrl" value="jdbc:h2:mem:qalatraining;DB_CLOSE_DELAY=-1"/>
        <property name="driverClass" value="org.h2.Driver"/>
        <property name="user" value="sa"/>
        <property name="password" value=""/>
        <property name="maxPoolSize" value="10"/>
        <property name="idleConnectionTestPeriod" value="3600"/>
        <property name="testConnectionOnCheckin" value="true"/>
    </bean>
    

    现在您应该将此 DataSource 传递给 Hibernate:

    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    

    配置数据库池还有很多其他内容,您可以找到详细信息 here 和工作示例 here。

    【讨论】:

      猜你喜欢
      • 2015-01-18
      • 2016-03-23
      • 2018-04-18
      • 2018-01-05
      • 2022-01-07
      • 2014-09-02
      • 2016-04-01
      • 2020-01-13
      • 2013-01-11
      相关资源
      最近更新 更多