zoukankan      html  css  js  c++  java
  • Hibernate的一级缓存和二级缓存

    • Fist level cache: This is enabled by default and works in session scope. Read more about hibernate first level cache.
    • Second level cache: This is apart from first level cache which is available to be used globally in session factory scope.

    一级缓存(不能disable):

    1. First level cache is associated with “session” object and other session objects in application can not see it.
    2. The scope of cache objects is of session. Once session is closed, cached objects are gone forever.
    3. First level cache is enabled by default and you can not disable it.
    4. When we query an entity first time, it is retrieved from database and stored in first level cache associated with hibernate session.
    5. If we query same object again with same session object, it will be loaded from cache and no sql query will be executed.
    6. The loaded entity can be removed from session using evict() method. The next loading of this entity will again make a database call if it has been removed using evict() method.
    7. The whole session cache can be removed using clear() method. It will remove all the entities stored in cache.

    二级缓存默认是disable,需要自己配置

                                                          

    How second level cache works:

    Lets write all the facts point by point:

    1. Whenever hibernate session try to load an entity, the very first place it look for cached copy of entity in first level cache (associated with particular hibernate session).
    2. If cached copy of entity is present in first level cache, it is returned as result of load method.
    3. If there is no cached entity in first level cache, then second level cache is looked up for cached entity.
    4. If second level cache has cached entity, it is returned as result of load method. But, before returning the entity, it is stored in first level cache also so that next invocation to load method for entity will return the entity from first level cache itself, and there will not be need to go to second level cache again.
    5. If entity is not found in first level cache and second level cache also, then database query is executed and entity is stored in both cache levels, before returning as response of load() method.
    6. Second level cache validate itself for modified entities, if modification has been done through hibernate session APIs.
    7. If some user or process make changes directly in database, the there is no way that second level cache update itself until “timeToLiveSeconds” duration has passed for that cache region. In this case, it is good idea to invalidate whole cache and let hibernate build its cache once again. You can use below code snippet to invalidate whole hibernate second level cache.

    refer:https://howtodoinjava.com/hibernate/understanding-hibernate-first-level-cache-with-example/

    https://howtodoinjava.com/hibernate/how-hibernate-second-level-cache-works/

    https://javarevisited.blogspot.com/2017/03/difference-between-first-and-second-level-cache-in-Hibernate.html

  • 相关阅读:
    Cassandra
    【CISCO强烈推荐】生成树 《路由协议》 卷一二 拥塞:网络延迟 阻塞:进程中 MTU QS:服务质量 OSPF RIP ISIS BGP 生成树 《路由协议》 卷一二
    m*n matrix min rank square matrix
    Moving Computation is Cheaper than Moving Data
    SASL mechanism
    一阶 斜率 二阶 原函数的粗糙度 roughness
    Linux 虚拟内存盘
    Bloom Filters
    R Tree
    释放内存
  • 原文地址:https://www.cnblogs.com/daxiong225/p/12574152.html
Copyright © 2011-2022 走看看