本文介绍了Hibernate缓存策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何决定使用哪个 CacheConcurrencyStrategy


  • NonstrictReadWriteCache

  • ReadOnlyCache
  • code> ReadWriteCache ,
  • TransactionalCache
  • ul>

    我读了,但没有详细解释。

    解决方案

    在定义它们方面做得很好:

    换言之:


    • 只读:适用于频繁读取但从未更新的数据(例如参考数据,如国家/地区)。很简单。它有最好的表现(显然)。 阅读/写入:如果您的数据需要更新,则可取。但它不提供隔离级别,可能发生(您可能会在交易结束时看到某些东西这在开始时并不存在)。它具有比只读更多的开销。

    • 不严格读/写:或者,如果不太可能两个单独的事务线程更新同一对象,则可以使用非严格读写策略。它的开销比读写少。这对于很少更新的数据非常有用。 只适用于JTA环境。




    因此,选择正确的策略取决于数据正在更新或而不是更新的频率和所需的隔离级别。如果您不知道如何回答这些问题以获取要缓存的数据,可以向DBA寻求支持。


    How do I decide which CacheConcurrencyStrategy to use?

    • NonstrictReadWriteCache,
    • ReadOnlyCache,
    • ReadWriteCache,
    • TransactionalCache.

    I read https://www.hibernate.org/hib_docs/v3/api/org/hibernate/cache/CacheConcurrencyStrategy.html, but doesn't explain in detail enough.

    解决方案

    The Hibernate documentation does a pretty good job at defining them:

    In other words:

    • Read-only: Useful for data that is read frequently but never updated (e.g. referential data like Countries). It is simple. It has the best performances of all (obviously).

    • Read/write: Desirable if your data needs to be updated. But it doesn't provide a SERIALIZABLE isolation level, phantom reads can occur (you may see at the end of a transaction something that wasn't there at the start). It has more overhead than read-only.

    • Nonstrict read/write: Alternatively, if it's unlikely two separate transaction threads could update the same object, you may use the nonstrict–read–write strategy. It has less overhead than read-write. This one is useful for data that are rarely updated.

    • Transactional: If you need a fully transactional cache. Only suitable in a JTA environment.

    So, choosing the right strategy depends on the fact that data are being updated or not, the frequency of updates and the isolation level required. If you don't know how to answer these questions for the data you want to put in cache, maybe ask some support from a DBA.

    这篇关于Hibernate缓存策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 13:13