Jpa中QueryPlanCache查询计划JPQL缓存

// 所以,项目大,缓存的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);
    }

}

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值