大家好哇,我是梦辛工作室的灵,在最近的工作中,又遇到了一个bug,系统运行一段时间后就会频繁OOM,然后直接假死或退出,一度折磨着我,后面不停的翻日志查GC,最后才终于确认位置,大家先看下下面的代码:
public Customer queryByOpenIDorUnionId(String openid, String unionId) {
Map<String, String> rp = new HashMap();
rp.put("unionid", unionId);
rp.put("openid", openid);
Customer customer = this.customerMapper.selectByUnionIdOrOpenId(rp);
this.fillRemainnMoney(customer);
return customer;
}
<select id="selectByUnionIdOrOpenId" parameterType="map" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from customer_info
<where>
<if test="unionid != null and unionid!=''">
and unionid = #{unionid}
</if>
<if test="openid!=null and openid!=''">
and openid = #{openid}
</if>
</where>
</select>
其实你晃眼一看感觉没什么问题对吧,就是一个简单的查询,但是你有没有想到万一传进来的值 openid 和 unionid 都为null的时候,就有问题了,这里的mysql就回去查询全表,如果你表里面的数据较多的话,例如50多万条数据,那么这里就可能会不断映射对象,创建50多万的对象,这是致命的,所以像这里如果确定返回记录就只有一条,最好 加上limit 1,在调用方法的时候也一定要先判空,不然后续找bug的时候会异常痛苦=-= 收,大家一定要注意这些小细节哇