简体   繁体   English

分布式环境中的JPA PersistenceContext

[英]JPA PersistenceContext in a distributed environment

Based on my understanding, transactions are not flushed immediately once they are completed. 根据我的理解,交易一旦完成就不会立即刷新。 They sit in a cache in memory and only get written to the DB when the EntityManager determines that it is cost effective to do so. 它们位于内存中的缓存中,只有当EntityManager确定这样做是符合成本效益时才会写入数据库。 I believe the L1 cache is utilized in this case, but correct me if I'm wrong. 我相信在这种情况下使用L1缓存,但如果我错了,请纠正我。

My question is, in a distributed environment, is the cache used by the Persistence Context distributed? 我的问题是,在分布式环境中,持久化上下文使用的缓存是分布式的吗?

L1 cache (session cache, persistence context) always works the same way, no matter wheteher your environment is distributed or not. 无论您的环境是否分布,L1缓存(会话缓存,持久性上下文)始终以相同的方式工作。 Session cache belongs to a session, and you can have multiple sessions, either on the same machine or on different machines, so that it doesn't matter. 会话缓存属于会话,您可以在同一台计算机上或不同的计算机上拥有多个会话,因此无关紧要。

In distributed environment you need to care about the second level cache, if you use it. 在分布式环境中,如果使用二级缓存,则需要关注它。

If you run your application in a cluster, you need to use cluster-capable L2 cache implementation, if you JPA provider supports it (see, for example, 21.2. The Second Level Cache from Hibernate documentation). 如果您在群集中运行应用程序,则需要使用支持群集的L2缓存实现(如果JPA提供程序支持它)(例如,请参阅21.2。来自Hibernate文档的二级缓存 )。

If you have other applications accessing the same database, you need to carefully configure caching strategies to avoid inconsistency in critical cases and tolerate possible inconsistency in other cases. 如果您有其他应用程序访问同一数据库,则需要仔细配置缓存策略以避免在关键情况下出现不一致,并在其他情况下容忍可能的不一致。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM