昨天在调试一个接口的时候,由于入参的字符串太长了,然后爆了一个问题,具体的日志如下:

org.springframework.dao.TransientDataAccessResourceException: \n###
Error updating database.
Cause: com.mysql.jdbc.PacketTooBigException: Packet for query is too large (1508792 > 1048576).
You can change this value on the server by setting the max_allowed_packet' variable.\n###
The error may involve com.wdzj.thirdpartzx.core.dao.OrderDetailMapper.insertSelectiveAndGetId-Inline\n###
The error occurred while setting parameters\n###
SQL: insert into tb_order ( owner_id, amount, status, plat_id, endpoint_id, sid, adder, add_time, request_info ) values ( ?, ?, ?, ?, ?, ?, ?, ?, ? )\n###
Cause: com.mysql.jdbc.PacketTooBigException: Packet for query is too large (1508792 > 1048576).
You can change this value on the server by setting the max_allowed_packet' variable.\n; SQL [];
Packet for query is too large (1508792 > 1048576).
You can change this value on the server by setting the max_allowed_packet' variable.; nested exception is com.mysql.jdbc.PacketTooBigException:
Packet for query is too large (1508792 > 1048576).
You can change this value on the server by setting the max_allowed_packet' variable.,
org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:106)

看到这个日志的关键词Packet for query is too large(1508792 > 1048576),看这意思貌似是传给MySQL解析

器的包太大了,默认是1M大小,1048576换算下来真多是1M啊,而我们传的参数数据包的大小1508792字节,换算下大概是1.43M,超过了默认的数据包的大小,怎么解决呢?日志中已经给出了You can change this value on the server by setting the max_allowed_packet’ variable,然后我自己大胆的尝试并验证有如下解决方案:
查看了下Mysql的配置文件 my.cnf,看到有一行配置max_allowed_packet = 1M ,把它修改为4M或者更多,然后重启了下Mysql服务,然后登录数据库查询了下:
show VARIABLES like ‘%max_allowed_packet%’;
查询数据库运行传过来包的大小
4194304换算下来就是4M,再次试了下,原来的插入语句可以正常使用。