使用mysql的java web应用程序中的“查询包过大”

我在ubuntu上的tomcat 7上运行web应用程序并连接到同一台机器上的mysql数据库.管理连接我正在使用Hikari数据源,我使用Hibernate作为ORM.

运行服务器几天后,我开始在日志文件中出现以下问题,所有请求都开始失败

Caused by: com.mysql.jdbc.PacketTooBigException: Packet for query is too large (1317 > 1024). You can change this value on the server by setting the max_allowed_packet' variable.

我在mysql服务器中设置了max allowed packet变量,如下所示[mysqld]部分.

key_buffer              = 16M
max_allowed_packet      = 200M
thread_stack            = 192K
thread_cache_size       = 8

但是我几天都会遇到这个问题,我需要重新启动tomcat,有时还需要重启mysql服务器,一旦重新启动它再运行几天.

在客户实体经理工厂我有以下

    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.***" />

    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
            p:databasePlatform="org.hibernate.dialect.MySQL5InnoDBDialect"
            p:showSql="false" />
    </property>

    <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
    </property>

    <property name="jpaPropertyMap">
        <map>
            <entry key="javax.persistence.validation.factory" value-ref="validator" />
            <entry key="hibernate.archive.autodetection" value="false" />
            <entry key="hibernate.format_sql" value="false" />
            <entry key="hibernate.generate_statistics" value="false" />
            <entry key="hibernate.hbm2ddl.auto" value="${hibernate.hbm2ddl.auto}" />
            <entry key="hibernate.connection.charSet" value="UTF-8" />
            <entry key="hibernate.bytecode.use_reflection_optimizer"
                value="true" />
            <entry key="hibernate.jdbc.use_scrollable_resultset" value="false" />
            <entry key="hibernate.jdbc.use_streams_for_binary" value="true" />
            <entry key="hibernate.jdbc.fetch_size" value="10" />
            <entry key="hibernate.jdbc.batch_size" value="30" />
            <entry key="hibernate.jdbc.batch_versioned_data" value="true" />
            <entry key="hibernate.default_batch_fetch_size" value="16" />
            <entry key="hibernate.order_inserts" value="true" />
            <entry key="hibernate.order_updates" value="true" />
            <entry key="hibernate.connection.max_allowed_packet" value="16905662" />
        </map>
    </property>
</bean>

我已经遵循了一些指南并配置了max_allowed_pa​​cket.但仍然遇到同样的问题.我在这里做错了吗?

最佳答案
我最近遇到了同样的问题,但更改MySql配置max_allowed_pa​​cket = 128M然后重新启动MySql服务,因为描述here解决了问题

转载注明原文:使用mysql的java web应用程序中的“查询包过大” - 代码日志