// 所以,项目大,缓存的JPQL多,占用的堆空间也多
// 在in的场景下,可能会出现内存泄露
// 因为各种Repository的查询,并且随着in的参数个数不同,queryPlanCache缓存的in的sql越来越多,这样时间久了就会出现无法释放,甚至可能出现OOM
// 我们可以通过hibernate.query.plan_cache_max_size和hibernate.query.plan_parameter_metadata_max_size来修改
// 还可以使用hibernate.query.in_clause_parameter_padding: true配置,减少in生成的缓存个数,根据参数格式的几何算法进行生成缓存,例如:生成2个参数,4个参数,2^2个参数的sql
class QueryPlanCache {
// JPQL的缓存信息
private final BoundedConcurrentHashMap queryPlanCache;
// 参数的缓存
private final BoundedConcurrentHashMap<ParameterMetadataKey, ParameterMetadataImpl> parameterMetadataCache;
// 在SessionFactoryImpl中创建的QueryPlanCache
// 所以,整了SessionFactory中,QueryPlanCache是单例的
public QueryPlanCache(final SessionFactoryImplementor factory) {
this.factory = factory;
// hibernate.query.plan_cache_max_size: 缓存占用的内存大小
Integer maxQueryPlanCount = ConfigurationHelper.getInteger(Environment.QUERY_PLAN_CACHE_MAX_SIZE,factory.getProperties());
// hibernate.query.plan_parameter_metadata_max_size: 缓存参数的占用内存大小
Integer maxParameterMetadataCount = ConfigurationHelper.getInteger(Environment.QUERY_PLAN_CACHE_PARAMETER_METADATA_MAX_SIZE,factory.getProperties());
this.queryPlanCache = new BoundedConcurrentHashMap(maxQueryPlanCount, 20, BoundedConcurrentHashMap.Eviction.LIRS);
this.parameterMetadataCache = new BoundedConcurrentHashMap<>(maxParameterMetadataCount, 20, BoundedConcurrentHashMap.Eviction.LIRS);
}
}
Jpa中QueryPlanCache查询计划JPQL缓存
最新推荐文章于 2024-04-29 07:45:00 发布