gpt4 book ai didi

java - EHCache 是如何实现它的事务的?

转载 作者:行者123 更新时间:2023-11-30 09:53:09 26 4
gpt4 key购买 nike

这个问题可能听起来很模糊,但我正在尝试理解 EHCache 事务能力的一般概念。
假设我将 EHCache 配置为内存缓存,并将其配置为缓存 MyObject
如果这是作为事务的一部分完成的,EHCache 是否会克隆我正在检索的 MyObject 实例?

我问这个问题主要是因为有人建议我(在我的 question 的回答中)使用 EHCache,我担心它对性能的影响。 MyObject 是一个中等重量的对象,我不想不必要地复制它。
另外,为了验证一下,EHCache 只在事务中阻止写入对象,对吗?

谢谢,
一泰

最佳答案

我认为有关 JTA 的文档的以下部分支持回答您的大部分问题:

Using a JTA Cache

All or nothing

If a cache is enabled for JTA all operations on it must happen within a transaction context, otherwise a TransactionRequiredException will be thrown.

Change Visibility

The isolation level offered in Ehcache JTA is READ_COMMITTED. Ehcache is an XAResource. Full two-phase commit is supported.

Specifically:

  • All mutating changes to the cache are transactional including put, remove, putWithWriter, removeWithWriter and removeAll.
  • Mutating changes are not visible in the local JVM to or across the cluster until COMMIT has been called.
  • Until then, read such as by cache.get(...) by other transactions will return the old copy. Reads do not block.

Write-behind and Write-through

If your XA enabled cache is being used with a writer, write operations will be queued until transaction commit time. Solely a Write-through approach would have its potential XAResource participate in the same transaction. Write-behind, while supported, should probably not be used with an XA transactional Cache, as the operations would never be part of the same transaction. Your writer would also be responsible for obtaining a new transaction...

Using Write-through with a non XA resource would also work, but there is no guarantee the transaction will succeed after the write operation have been executed successfully. On the other hand, any thrown exception during these write operations would cause the transaction to be rolled back by having UserTransaction.commit() throw a RollbackException.

关于性能,除非您的对象有数百 MB,否则我不会太担心。但如果这真的是一个问题,请衡量事物(一如既往)。

关于java - EHCache 是如何实现它的事务的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3967111/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com